initialize vector elements

Discussion in 'C++' started by peter.vanna@gmail.com, Apr 18, 2007.

  1. Guest

    Hi,

    I have the following vector container.

    ***************
    class A
    {
    public:
    bool test;
    }

    vector<A> v

    **************

    I would like to set the "test" variable to be false for all the
    elements in v. A simple code is

    for(i=0;i<v.size;i++)
    {
    v.test=false;
    }

    Is there other approach (or built-in function) to do the same function
    as the above for-loop? Thanks.
     
    , Apr 18, 2007
    #1
    1. Advertising

  2. wrote:
    > I have the following vector container.
    >
    > ***************
    > class A
    > {
    > public:
    > bool test;
    > }
    >
    > vector<A> v
    >
    > **************
    >
    > I would like to set the "test" variable to be false for all the
    > elements in v. A simple code is
    >
    > for(i=0;i<v.size;i++)
    > {
    > v.test=false;
    > }
    >
    > Is there other approach (or built-in function) to do the same function
    > as the above for-loop? Thanks.


    The simplest way is to create a constructor for A that would do the
    initialisation the way you want, and then use copy-construction like
    so:

    class A {
    public:
    A(bool t) : test(t) {} // the c-tor
    bool test;
    };
    ...

    vector<A>(v.size(), A(false)).swap(v);

    or

    v = vector<A>(v.size(), A(false));

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Apr 18, 2007
    #2
    1. Advertising

  3. Jerry Coffin Guest

    In article <>,
    says...
    > Hi,
    >
    > I have the following vector container.
    >
    > ***************
    > class A
    > {
    > public:
    > bool test;
    > }
    >
    > vector<A> v
    >
    > **************
    >
    > I would like to set the "test" variable to be false for all the
    > elements in v. A simple code is
    >
    > for(i=0;i<v.size;i++)
    > {
    > v.test=false;
    > }
    >
    > Is there other approach (or built-in function) to do the same function
    > as the above for-loop? Thanks.


    There are a couple of possibilities. One is to have vector create your
    vector containing copies of an object you supply:

    A a;

    a.test = false;

    vector<A> v(size, a);

    Another is to supply a default ctor that creates your objects with the
    member set to false:

    class A {
    bool test;
    public:
    A(bool value = false) : test(value) { }
    };

    Then objects in the vector will be created with test set to false, just
    like any other object of the same type would.

    Note that neither of these does quite the same as the for loop (at least
    currently appears to do). Unless you have some intervening code to set
    the size of your vector to 'size' (or larger) your code writes beyond
    the end of the vector's allocation, causing undefined behavior.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Apr 19, 2007
    #3
  4. James Kanze Guest

    On Apr 18, 11:53 pm, wrote:

    > I have the following vector container.


    > ***************
    > class A
    > {
    > public:
    > bool test;
    > }


    > vector<A> v
    > **************


    > I would like to set the "test" variable to be false for all the
    > elements in v. A simple code is


    > for(i=0;i<v.size;i++)
    > {
    > v.test=false;
    >
    > }


    > Is there other approach (or built-in function) to do the same function
    > as the above for-loop?


    If the test variable is the only member (or if you want to
    change the value of all of the members of the class):
    A anA ;
    anA.test = false ;
    std::fill( v.begin(), v.end(), anA ) ;
    even better would be to provide a constructor for the class, so
    you can write:
    std::fill( v.begin(), v.end(), A( false ) ) ;

    If A has other members which you don't want to modify, you're
    stuck with a loop, unless you use something like boost::bind.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Apr 19, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. hyena
    Replies:
    10
    Views:
    15,384
  2. pmatos
    Replies:
    6
    Views:
    23,826
  3. Replies:
    8
    Views:
    1,930
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    570
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    366
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page