Thread: Issue filling a vector with elements

  1. #1
    Guest
    Guest

    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
    28,413
    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);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Guest
    Guest
    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
    257
    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
    4,633
    You could also initialize the vector in the initialization list:
    Code:
    World::World(const u_short worldsize) : grid((worldsize * worldsize), false)
    {
    Jim

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh yes, it is a constructor, so initialising the vector rather than using the assign member function should be preferred.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 10: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, 07:19 PM