On Apr 18, 11:53 pm, peter.va...@gmail.com wrote:
I have the following vector container.
***************
class A
{
public:
bool test;
}
vector<Av
**************
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[i].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:ja*********@gmail.com
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