Sinisterly
I Need Help With This Vector Problem - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: C, C++, & Obj-C (https://sinister.ly/Forum-C-C-Obj-C)
+--- Thread: I Need Help With This Vector Problem (/Thread-I-Need-Help-With-This-Vector-Problem)

Pages: 1 2


RE: I Need Help With This Vector Problem - phyrrus9 - 08-09-2019

(08-08-2019, 04:47 PM)Drako Wrote:
(08-08-2019, 04:02 AM)phyrrus9 Wrote:
(08-07-2019, 08:02 PM)Drako Wrote: I started doing that a while back when I read that endl was actually inefficient. What I read, and also heard from a lot of people was that it flushes and creates a newline. Which is unnecessary.

You'd be correct on it being inefficient, but the entirety of cout is that way. As for flushing the stream though, that's what std::flush is for.

I had already built the entire program with cout so I didn't want to try and use printf. Printf is a lot faster. As for my problem, any solutions?

which problem was that?


RE: I Need Help With This Vector Problem - Drako - 08-09-2019

(08-09-2019, 07:20 PM)phyrrus9 Wrote:
(08-08-2019, 04:47 PM)Drako Wrote:
(08-08-2019, 04:02 AM)phyrrus9 Wrote: You'd be correct on it being inefficient, but the entirety of cout is that way. As for flushing the stream though, that's what std::flush is for.

I had already built the entire program with cout so I didn't want to try and use printf. Printf is a lot faster. As for my problem, any solutions?

which problem was that?

My reply there. Do you at least have an idea on how I can solve it?


RE: I Need Help With This Vector Problem - phyrrus9 - 08-09-2019

(08-09-2019, 10:38 PM)Drako Wrote:
(08-09-2019, 07:20 PM)phyrrus9 Wrote:
(08-08-2019, 04:47 PM)Drako Wrote: I had already built the entire program with cout so I didn't want to try and use printf. Printf is a lot faster. As for my problem, any solutions?

which problem was that?

My reply there. Do you at least have an idea on how I can solve it?

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


RE: I Need Help With This Vector Problem - Drako - 08-09-2019

(08-09-2019, 11:04 PM)phyrrus9 Wrote:
(08-09-2019, 10:38 PM)Drako Wrote:
(08-09-2019, 07:20 PM)phyrrus9 Wrote: which problem was that?

My reply there. Do you at least have an idea on how I can solve it?

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

I quoted the wrong post...

I meant my code, and not cout and printf.

Code:
std::vector<int> infectChance = {5, 10, 15, 20, 25, 30, 35, 40, 45}; //List of possible infection chances

    std::cout << "  1 > Kin | Location Size: Large | Infection Chance: " << infectChance[0] << "%" << '\n' //Prints everything
                << "  Location Description - Kin is the largest location in the region, you'll" << '\n'
                << "  probably find lots of loot, but just as many bad guys.\n" << '\n'
                << "  2 > Military Outpost | Location Size: Large | Infection Chance: " << infectChance[1] << "%" << '\n'
                << "  Location Description - The government let us down. So does that mean" << '\n'
                << "  you can take their stuff now? Hell yes!\n" << '\n'
                << "  3 > Vernal | Location Size: Medium | Infection Chance: " << infectChance[2] << "%" << '\n'
                << "  Location Description - A pretty under-developed, rundown town." << '\n'
                << "  It's still got stuff though. Maybe.\n" << '\n'
                << "  4 > Mason | Location Size: Medium | Infection Chance: " << infectChance[3] << "%" << '\n'
                << "  Location Description - A better more developed Vernal. Go check it" << '\n'
                << "  out.\n" << '\n'
                << "  5 > Mavren | Location Size: Small | Infection Chance: " << infectChance[4] << "%" << '\n'
                << "  Location Description - OK, lets just make this short and sweet. it's" << '\n'
                << "  trailer park. Don't expect a thing.\n" << '\n'
                << "  6 > Return To Bunker\n" << '\n';



RE: I Need Help With This Vector Problem - Drako - 10-14-2019

(08-09-2019, 11:04 PM)phyrrus9 Wrote:
(08-09-2019, 10:38 PM)Drako Wrote:
(08-09-2019, 07:20 PM)phyrrus9 Wrote: which problem was that?

My reply there. Do you at least have an idea on how I can solve it?

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?


RE: I Need Help With This Vector Problem - phyrrus9 - 10-14-2019

(10-14-2019, 07:02 PM)Drako Wrote:
(08-09-2019, 11:04 PM)phyrrus9 Wrote:
(08-09-2019, 10:38 PM)Drako Wrote: My reply there. Do you at least have an idea on how I can solve it?

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
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));



RE: I Need Help With This Vector Problem - Drako - 10-15-2019

(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
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.


RE: I Need Help With This Vector Problem - phyrrus9 - 10-15-2019

(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.