# Two newbie questions regarding the vector class

Printable View

• November 29th, 2011, 11:07 PM
Ulnarian
Two newbie questions regarding the vector class
Hey all,

Just for fun, I've decided to try to design a card game. However, as I'm pretty new at this, I'm taking baby steps. Anyways, the problem I have right now is how to generate 5 unique, random numbers from a predefined array.

Someone suggested that I check out the vector class and it seems to do what I want rather nicely. Right now, I am using random_shuffle() to randomly sort the elements of a predefined array. Here's my code:

Code:

``` #include <iostream> #include <vector> using namespace std; int main() { vector <int> cards(5); cards[0]=1; cards[1]=2; cards[2]=3; cards[3]=4; cards[4]=5; random_shuffle (cards.begin(), cards.end()); int na_playerhand[5]; for (int i=0; i<5, i++) { na_playerhand[i]=cards[i]; cout << playerhand[i]' } return 0; }```
While this code works great the first time around (i.e. it outputs the numbers 1-5 in random order, for example "35421"), for each subsequent time I run the program, I get the same sequence of numbers (i.e. the output is always "35421")---what gives?

As for the second question, my initialization of the array seems rather cumbersome, is there some kind of shortcut to initializing a vector array?

Thanks!
• November 29th, 2011, 11:10 PM
laserlight
Re: Two newbie questions regarding the vector class
You did not seed the pseudorandom number generator that random_shuffle uses, so it used the default seed on each run of the program. Hence, use srand at the start of the main function, possibly with a seed derived from current time or some random source.
• November 30th, 2011, 08:22 AM
Eri523
Re: Two newbie questions regarding the vector class
Quote:

Originally Posted by Ulnarian
As for the second question, my initialization of the array seems rather cumbersome, is there some kind of shortcut to initializing a vector array?

You may use a loop to initialize your vector. Either construct the vector with the desired number of items as you do now and then write each item in a loop, or default-construct the vector (i.e. without passing the number of items upon construction) and then add the items in the loop using the vector's push_back() member. Personaly, I prefer the second approach. While it actually may be a bit less efficient performance-wise, it's easier to write, IMO more elegant and safer.
• November 30th, 2011, 10:49 AM
laserlight
Re: Two newbie questions regarding the vector class
Quote:

Originally Posted by Eri523
You may use a loop to initialize your vector. Either construct the vector with the desired number of items as you do now and then write each item in a loop, or default-construct the vector (i.e. without passing the number of items upon construction) and then add the items in the loop using the vector's push_back() member.

Another way is to use Boost.Iterator's counting_iterator.
• November 30th, 2011, 02:50 PM
Eri523
Re: Two newbie questions regarding the vector class
Actually, I figured there would be some fancy STL way of doing that. I was just too lazy to figure it out while I could present the old-school loop-based approach off the top of my head. :o I entirely failed to consider boost for that, though. And the counting iterator perfectly fits the scenario here.

And now you've callanged me. :cool: I tried to find a way to do that without a third-party library. First I thought of something with a lamda that may effectively turn that into a one-liner, but I'm still not familiar with the lambda syntax. So I decided for a functor-based approach, creating something like a poor-man's repacement of the counting iterator:

Code:

```// Test18.cpp #include <vector> #include <algorithm> #include <iostream> template<typename T> class Incrementer { public:   Incrementer(T start) : i(start) {}   T operator()() { return i++; } private:   T i; }; int main() {   std::vector<int> cards(5);   std::generate(cards.begin(), cards.end(), Incrementer<int>(1));   // However, now I'm resorting to the old-school way for outputting the vector - too lazy to look that up as well... <g>   for (int i = 0; i < 5; ++i)     std::cout << cards[i] << ' ';   std::cout << std::endl;   return 0; }```
And unlike the lambda approach, this doesn't even require C++11! :D
• November 30th, 2011, 03:11 PM
Peter_B
Re: Two newbie questions regarding the vector class
@Eri523:

You can replace the output loop:
Code:

```for (int i = 0; i < 5; ++i)   std::cout << cards[i] << ' ';```
with
Code:

`std::copy(cards.begin(), cards.end(), std::ostream_iterator<int>(cout, " "));`
• November 30th, 2011, 03:22 PM
Eri523
Re: Two newbie questions regarding the vector class
Thanks for adding that Peter_B. That's exactly what I was too lazy to look up. :rolleyes: Actually, I come across practically that same line every once in a while and still haven't managed to properly memorize it... :blush: