Like Tree4Likes
  • 1 Post By laserlight
  • 3 Post By jimblumberg

Issue filling a vector with elements

This is a discussion on Issue filling a vector with elements within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm writing a program with a class containing a private std::vector<bool> . I chose bool because the vector represents ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    20

    Issue filling a vector with elements

    Hello,

    I'm writing a program with a class containing a private std::vector<bool>. I chose bool because the vector represents a 2D array (think grid) and I only need 2 states per cell. I kept it one-dimensional as this hardly complicates things.

    My problem is that I don't know how to initialize the vector, i.e. fill it with 0's.

    The grid's resolution is not known at compile time, so I imagine I have to set the size (and content) of the vector in the class constructor.

    Here's what I have tried among several things:

    Code:
    World::World(const u_short worldsize)
    {
        grid.reserve(worldsize * worldsize); // grid is the private vector; square dimensions.
        std::fill(grid.begin(), grid.end(), 0);
        std::cout << grid.size();
    }
    The output is 0. Only std::vector::push_back seems to have an effect on size(), but judging by its description, it doesn't look like the right candidate to populate a vector with zeros. Correct me if I'm wrong.

    Frankly I expected line 3 to set the vector's size.

    Any suggestions appreciated, also regarding alternative containers (bitset, map). The vector will be read from very frequently, and written to on occasion. Thanks.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,759
    The reserve member function reserves capacity. It does not set the size of the vector. Rather, you can replace the reserve and fill with:
    Code:
    grid.assign(worldsize * worldsize, false);
    -Adrian likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2013
    Location
    Germany
    Posts
    20
    Thank you, that does it. Just read up on std::vector::assign. Problem solved!

  4. #4
    Registered User antred's Avatar
    Join Date
    Apr 2012
    Location
    Germany
    Posts
    251
    Alternatively, you could use

    Code:
    grid.resize( worldsize * worldsize, false /* <- default value to assign to new elements added by this call */ );
    but note that this is functionally equivalent to laserlight's code only if you start out with an empty vector.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    You could also initialize the vector in the initialization list:
    Code:
    World::World(const u_short worldsize) : grid((worldsize * worldsize), false)
    {
    Jim
    antred, laserlight and -Adrian like this.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,759
    Oh yes, it is a constructor, so initialising the vector rather than using the assign member function should be preferred.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with Filling Array
    By sam.briggs in forum C Programming
    Replies: 9
    Last Post: 09-05-2011, 12:16 PM
  2. vector of 2d array elements?
    By jmartrican in forum C++ Programming
    Replies: 6
    Last Post: 10-02-2007, 09:05 PM
  3. Vector elements
    By strickey in forum C++ Programming
    Replies: 5
    Last Post: 02-04-2005, 09:30 AM
  4. Problem Filling Vector
    By smitsky in forum C++ Programming
    Replies: 2
    Last Post: 10-10-2004, 01:07 AM
  5. Filling a vector from file... having a hard time...
    By criticalerror in forum C++ Programming
    Replies: 7
    Last Post: 12-11-2003, 06:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21