C++: Insert vector into anonymous vector, recursively





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















My goal is to generate vector with pattern {1} given max_=2, {1,2,1} given max_=3, {1,2,1,3,1,2,1} given max_=4... etc



Here is my psuedocode:



std::vector<int> generate_zimin(int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
return {generate_zimin(max_-1), (max_-1), generate_zimin(max_-1)};
}
}


How do I insert vectors into an anonymous vector?



edit:



std::vector<int> generate_zimin(std::vector<int> current, int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
std::vector<int> prev = generate_zimin(current, max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
current.push_back(max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
return current;
}
}


looks a lot less elegant, and I was just trying to avoid that, but I can see
why I was wrong.










share|improve this question

























  • You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

    – Igor Tandetnik
    Nov 24 '18 at 3:56











  • There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

    – alter igel
    Nov 24 '18 at 3:56











  • @IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

    – Bo Work
    Nov 24 '18 at 4:14


















0















My goal is to generate vector with pattern {1} given max_=2, {1,2,1} given max_=3, {1,2,1,3,1,2,1} given max_=4... etc



Here is my psuedocode:



std::vector<int> generate_zimin(int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
return {generate_zimin(max_-1), (max_-1), generate_zimin(max_-1)};
}
}


How do I insert vectors into an anonymous vector?



edit:



std::vector<int> generate_zimin(std::vector<int> current, int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
std::vector<int> prev = generate_zimin(current, max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
current.push_back(max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
return current;
}
}


looks a lot less elegant, and I was just trying to avoid that, but I can see
why I was wrong.










share|improve this question

























  • You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

    – Igor Tandetnik
    Nov 24 '18 at 3:56











  • There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

    – alter igel
    Nov 24 '18 at 3:56











  • @IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

    – Bo Work
    Nov 24 '18 at 4:14














0












0








0








My goal is to generate vector with pattern {1} given max_=2, {1,2,1} given max_=3, {1,2,1,3,1,2,1} given max_=4... etc



Here is my psuedocode:



std::vector<int> generate_zimin(int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
return {generate_zimin(max_-1), (max_-1), generate_zimin(max_-1)};
}
}


How do I insert vectors into an anonymous vector?



edit:



std::vector<int> generate_zimin(std::vector<int> current, int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
std::vector<int> prev = generate_zimin(current, max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
current.push_back(max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
return current;
}
}


looks a lot less elegant, and I was just trying to avoid that, but I can see
why I was wrong.










share|improve this question
















My goal is to generate vector with pattern {1} given max_=2, {1,2,1} given max_=3, {1,2,1,3,1,2,1} given max_=4... etc



Here is my psuedocode:



std::vector<int> generate_zimin(int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
return {generate_zimin(max_-1), (max_-1), generate_zimin(max_-1)};
}
}


How do I insert vectors into an anonymous vector?



edit:



std::vector<int> generate_zimin(std::vector<int> current, int max_)
{
if(max_ == 2)
{
return {1};
}
else
{
std::vector<int> prev = generate_zimin(current, max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
current.push_back(max_-1);
current.insert(current.end(), std::begin(prev), std::end(prev));
return current;
}
}


looks a lot less elegant, and I was just trying to avoid that, but I can see
why I was wrong.







c++ recursion vector






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 24 '18 at 4:48







Bo Work

















asked Nov 24 '18 at 3:54









Bo WorkBo Work

104




104













  • You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

    – Igor Tandetnik
    Nov 24 '18 at 3:56











  • There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

    – alter igel
    Nov 24 '18 at 3:56











  • @IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

    – Bo Work
    Nov 24 '18 at 4:14



















  • You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

    – Igor Tandetnik
    Nov 24 '18 at 3:56











  • There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

    – alter igel
    Nov 24 '18 at 3:56











  • @IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

    – Bo Work
    Nov 24 '18 at 4:14

















You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

– Igor Tandetnik
Nov 24 '18 at 3:56





You don't. Why does it have to be "anonymous"? What's wrong with using a local variable to accumulate the result?

– Igor Tandetnik
Nov 24 '18 at 3:56













There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

– alter igel
Nov 24 '18 at 3:56





There isn't really a way to do it in a one-liner. See this question about concatenating vectors: stackoverflow.com/questions/201718/concatenating-two-stdvectors

– alter igel
Nov 24 '18 at 3:56













@IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

– Bo Work
Nov 24 '18 at 4:14





@IgorTandetnik By using local variable, I should declare an empty vector at the start of the function, and append generate_zimin(max_-1) to it, then push_back max_-1 to it, then append generate_zimin(max_z-1), then return it?

– Bo Work
Nov 24 '18 at 4:14












1 Answer
1






active

oldest

votes


















0














Not only is there no syntax for this, it is a bad idea for performance. You'll be allocating and deallocating vectors on every function call. You should create a single vector at the beginning and use it in all the calls. This could improve performance by a factor of 100.



You can even calculate the total size of the vector at the beginning and reserve() the needed capacity.






share|improve this answer
























  • How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

    – Bo Work
    Nov 24 '18 at 4:10













  • Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

    – John Zwinck
    Nov 24 '18 at 4:24












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455022%2fc-insert-vector-into-anonymous-vector-recursively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Not only is there no syntax for this, it is a bad idea for performance. You'll be allocating and deallocating vectors on every function call. You should create a single vector at the beginning and use it in all the calls. This could improve performance by a factor of 100.



You can even calculate the total size of the vector at the beginning and reserve() the needed capacity.






share|improve this answer
























  • How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

    – Bo Work
    Nov 24 '18 at 4:10













  • Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

    – John Zwinck
    Nov 24 '18 at 4:24
















0














Not only is there no syntax for this, it is a bad idea for performance. You'll be allocating and deallocating vectors on every function call. You should create a single vector at the beginning and use it in all the calls. This could improve performance by a factor of 100.



You can even calculate the total size of the vector at the beginning and reserve() the needed capacity.






share|improve this answer
























  • How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

    – Bo Work
    Nov 24 '18 at 4:10













  • Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

    – John Zwinck
    Nov 24 '18 at 4:24














0












0








0







Not only is there no syntax for this, it is a bad idea for performance. You'll be allocating and deallocating vectors on every function call. You should create a single vector at the beginning and use it in all the calls. This could improve performance by a factor of 100.



You can even calculate the total size of the vector at the beginning and reserve() the needed capacity.






share|improve this answer













Not only is there no syntax for this, it is a bad idea for performance. You'll be allocating and deallocating vectors on every function call. You should create a single vector at the beginning and use it in all the calls. This could improve performance by a factor of 100.



You can even calculate the total size of the vector at the beginning and reserve() the needed capacity.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 24 '18 at 3:58









John ZwinckJohn Zwinck

155k17181300




155k17181300













  • How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

    – Bo Work
    Nov 24 '18 at 4:10













  • Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

    – John Zwinck
    Nov 24 '18 at 4:24



















  • How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

    – Bo Work
    Nov 24 '18 at 4:10













  • Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

    – John Zwinck
    Nov 24 '18 at 4:24

















How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

– Bo Work
Nov 24 '18 at 4:10







How would I reserve the needed capacity when its function is also recursive? A generate_zimin(n)'s total space = 2*generate_zimin(n-1)'s total space +1. I must be overthinking this too much

– Bo Work
Nov 24 '18 at 4:10















Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

– John Zwinck
Nov 24 '18 at 4:24





Total capacities are 1, 3, 7, 15, 31. Punch that into a search and voila: oeis.org/A000225 - it is 2^n - 1.

– John Zwinck
Nov 24 '18 at 4:24




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53455022%2fc-insert-vector-into-anonymous-vector-recursively%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()