dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7

Thread: Two newbie questions regarding the vector class

  1. #1
    Join Date
    May 2006
    Posts
    102

    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!

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,767

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Two newbie questions regarding the vector class

    Quote Originally Posted by Ulnarian View Post
    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.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #4
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,767

    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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  5. #5
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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. I entirely failed to consider boost for that, though. And the counting iterator perfectly fits the scenario here.

    And now you've callanged me. 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!
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  6. #6
    Join Date
    Jan 2009
    Posts
    596

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

  7. #7
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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. Actually, I come across practically that same line every once in a while and still haven't managed to properly memorize it...
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)