RE: I Need Help With This Vector Problem 10-15-2019, 04:47 AM
#17
(10-14-2019, 07:56 PM)phyrrus9 Wrote:(10-14-2019, 07:02 PM)Drako Wrote:(08-09-2019, 11:04 PM)phyrrus9 Wrote: Of using cout instead of printf? If it's not a real problem, I wouldn't worry about it. Going forward, if you can stand your codebase being unclean, use printf for new stuff. I was merely curious why you mixed the two
Code:std::vector<int> infectChance = {10, 15, 20, 25, 30, 35, 40, 45, 50};
infectChance.reserve(10);
if (shuffled == false)
{
shuffled = true;
unsigned seed = std::chrono::system_clock::now()
.time_since_epoch()
.count();
shuffle(infectChance.begin(), infectChance.end(), std::default_random_engine(seed));
}
I've changed a lot of the code. What I need to know now is how to save the elements in the vector after they have been shuffled. I need to know this because all of the code you see above is in a 'void' function. So once the 'void' function is called, everything in my vector is reset. Is there a way I can save the elements to memory?
Pass the vector by reference.
change
toCode:void shuffle(std::iterator begin, std::iterator end, whatever_that_type_was seed);
Code:void shuffle(std::vector<int> &vector, whatever_that_type_was seed);
and then get the begin and end iterators from within the function.
Oh, and of course, change your call to
Code:shuffle(&infectChance, std::default_random_engine(seed));
I feel genuinely stupid. I fixed my problem based a bit off of your answer. All I needed to do was declare the 'infectChance' vector outside of my void function. I guess I was trying to over complicate things. Thanks for your reply! I've been trying to fix this for about a month.