Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > initialize vector elements

Reply
Thread Tools

initialize vector elements

 
 
peter.vanna@gmail.com
Guest
Posts: n/a
 
      04-18-2007
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[i].test=false;
}

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

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      04-18-2007
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[i].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


 
Reply With Quote
 
 
 
 
Jerry Coffin
Guest
Posts: n/a
 
      04-19-2007
In article <. com>,
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[i].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.
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      04-19-2007
On Apr 18, 11:53 pm, peter.va...@gmail.com 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[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:
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
how the vector is created, how to pass vector to webservices method apachesoap:Vector Rushikesh Joshi Perl Misc 0 07-10-2004 01:04 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57