Problems with vector.push_back()

Started by
17 comments, last by alvaro 11 years ago

What exactly do you mean by "removing"? Removed as per SiCrane's suggestions or just removed it from your code?

If you do not supply one explicitly the compiler will generate one which might have similar problems depending on the situation.

I just removed it from my code and compiled generated constructors works just fine for me. One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Deltron Zero and Automator.

Advertisement

What exactly do you mean by "removing"? Removed as per SiCrane's suggestions or just removed it from your code?

If you do not supply one explicitly the compiler will generate one which might have similar problems depending on the situation.

I just removed it from my code and compiled generated constructors works just fine for me.

But as BitMaster said, the compiler generated copy constructor is as incorrect as the copy constructor you originally had. It doesn't work. Your class simply isn't set up for handling the ownership of the pointer, and neither the compiler generated copy constructor nor your original copy constructor is doing the right job. And by the way, this applies to the copy assignment operator as well.

The point is to completely disable copying by making the copy constructor and assignment operators private to ensure that you cannot copy the objects. Or, implement proper copy operators to ensure that the ownership of the pointer is handled correctly.

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.


srand(time(NULL));

kaktusas2598, on 28 Mar 2013 - 13:09, said:

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.

srand(time(NULL));

But I am using random_shuffle(). not rand(), does srand() also works with random_shuffle()? :o

Deltron Zero and Automator.

kaktusas2598, on 28 Mar 2013 - 13:09, said:

One more question. Do anybody know how use random_shuffle() which I am using to shuffle my deck to shuffle deck diferrently every time, similar like using srand() with rand(). Cause now it shuffles the deck same way every time I run the game, so I think I need some way to seed some random number based by time every time I use random_shuffle(). Thank you.

Seed the random number generator with something that is not pre-defined, such as time.

srand(time(NULL));

But I am using random_shuffle(). not rand(), does srand() also works with random_shuffle()? ohmy.png

Sorry, I was thinking randomness and seeding and wasn't exactly thinking about what I was actually answering. The random generator used by the two-parameter variant of std::random_shuffle is in fact implementation defined, but probably uses rand. If it does, then srand will seed std::random_shuffle.

However, you may want to use the variant with the third parameter instead, which lets you specify the random number generator yourself. I believe you can pass rand as the third parameter to make sure that it does, in fact, use rand for random numbers.

rand() isn't a valid function for the third parameter of std::random_shuffle(). If you pass it a function, the function has to take one argument n, and return a value from 0 to n - 1. rand() doesn't take any arguments.

I apologize, that makes total sense.

I have read on internet, that random_shuffle really was inplemented using rand(), so srand() works! Thank you, fellow developers, for all your help! smile.png

Deltron Zero and Automator.

It would be more accurate to say "is usually implemented using rand()". The C++ standard doesn't require standard library implementations to use rand() under the hood for the two argument version of the random_shuffle() function. I would be cautious about trusting an internet source that said this was definitely the case (unless it was referring to your particular compiler/standard library implementation).
I just checked a late draft of the C++11 standard, and SiCrane is correct. This is unfortunate, and it leaves us with two options: Either avoid using the two-argument version of random_shuffle (since the standard doesn't give you any tools to seed the source of randomness) or live our lives ignoring this oddity in the standard, and trusting that compiler writers are sensible people. If a compiler doesn't use rand as the source of randomness, I will happily stop using that compiler.

This topic is closed to new replies.

Advertisement