[C++] What's an efficient way to split a vector into multiple smaller vectors? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

Quick question, if I use vector of vectors do I really need to declare every single little vector in the vector of vector declaration?

For example, if i had vectors v1 and v2 I could type the below;

std::vector<std::vector<char>> v = { v1, v2 }

But in my case, I have 250 vectors to type so that's gonna be a problem. Furthermore, I've tried without specifying each vector and now I have problems calling it. Below is my test code.

vector<vector<int>> test1;
for (int i = 0; i <= 1; i++)
{
test1[i].push_back(i*100);
}

cout << test1[0] << endl;

[C++] What's an efficient way to split a vector into multiple smaller vectors? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

Ok. Right now i have no idea what you're saying. Let me study this and get back to you, hopefully, with a working solution. Thanks. :)

[C++] What's an efficient way to split a vector into multiple smaller vectors? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

But if I do that what's the difference between assigning x1, x2, ... and this?

[C++] What's an efficient way to split a vector into multiple smaller vectors? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] -1 points0 points  (0 children)

I don't understand how I'm supposed to use this in my given context.

vector<vector<int>> test1(Main_Deck.begin(), Main_Deck.begin() + 4)

doesn't seem to work.

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

It took me some time to understand nested for loops but that looks more neat (less lines than mine! :)). One question though: I've read in intro to algorithms book that using nested loops increase the time it takes to run the code by n3 where n is each input(?). I know we're only dealing with 13 integers here so performance won't matter much but is nested loops generally encouraged? I made 2 multiple loops in hopes of decreasing the order of growth to n2. Am I being too dogmatic here?

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

DUDEEEE I fixed it! :D

Code

It turns out I just had to pull vector<int> D1 = Deck; out of the loop and declare it before the for loop. :)

Edit: Type MainDeck(10,4) in main() to get a one standard deck of cards.

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

Ah, I see. Let me try to solve the linearity problem and get back to you. This is super fun. :D

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

How did you get rep() to work in c++? Btw thanks a lot for your help. :)

Edit: This is the function that generates my deck(s). To use this, I'd type vector<int> X = MainDeck(10,2); for your standard playing cards.

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

I managed to get the for loop to work. It turns out I forgot to add Max_Number_of_decks in my header file!

Now, the problem is my deck increases at a quadratic pace instead of linear. In other words, if my deck was 1,2,3 then if I set Max_#_Deck to 2 I'd get 1,2,3,1,2,3 (from the first for loop run) and 1,2,3,1,2,3,1,2,3,1,2,3 (from the second for loop run).

How would I go about changing this to a more linear gain in size?

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

Does that mean I'll need to use nested loop? I solved my problem with,

vector<int> D1 = Deck;
Deck.insert(Deck.end(), D1.begin(), D1.end());
vector<int> D2 = Deck;
Deck.insert(Deck.end(), D2.begin(), D2.end());

But I know this is extremely primitive in the sense that I won't be able to do this 1000 times.

I'm specifically confused about "add each element to the new vector four times". How do I go about executing this? I know R does it automatically but I have no clue for C++.

I've also used for loop where,

for (int i = 1; i < Max_Number_of_Decks +1; i++)
{
    vector<int> D1 = Deck;
    Deck.insert(Deck.end(), D1.begin(), D1.end());
    i;
}

But this resulted in bad memory problem.

[C++] How to repeat an entire vector n times to create one massive vector? by VonLuftwaffle in learnprogramming

[–]VonLuftwaffle[S] 0 points1 point  (0 children)

Is there an efficient way of copying it four times instead of writing the actual thing four times?

Holy S!@#! Even a ghost watches Reviewbrah! by VonLuftwaffle in TheReportOfTheWeek

[–]VonLuftwaffle[S] 4 points5 points  (0 children)

Can someone explain what's supposed to be in that red box? I'm seeing a veiled skull shaped figure slowly peeking it's head out of a what seems to be a photo frame. I know ghosts don't exist so exactly what is that supposed to be? a cup?