initialize vector elements


P

peter.vanna

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.
 
Ad

Advertisements

V

Victor Bazarov

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
 
J

Jerry Coffin

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.
 
Ad

Advertisements

J

James Kanze

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.
 

Top