Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


I Need Help With This Vector Problem filter_list
Author
Message
RE: I Need Help With This Vector Problem #18
(10-15-2019, 04:47 AM)Drako Wrote:
(10-14-2019, 07:56 PM)phyrrus9 Wrote:
(10-14-2019, 07:02 PM)Drako Wrote:
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
Code:
void shuffle(std::iterator begin, std::iterator end, whatever_that_type_was seed);
to
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.

No.....you didn't fix your problem, you made it worse. You shouldn't have any global variables. Pass them back and forth between your functions, this isn't Python.

Reply





Messages In This Thread
I Need Help With This Vector Problem - by Drako - 06-09-2019, 01:41 AM
RE: I Need Help With This Vector Problem - by phyrrus9 - 10-15-2019, 04:21 PM



Users browsing this thread: 2 Guest(s)