473,387 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

initialize vector elements

Hi,

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? Thanks.

Apr 18 '07 #1
3 2038
pe*********@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? 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
Apr 18 '07 #2
In article <11*********************@d57g2000hsg.googlegroups. com>,
pe*********@gmail.com says...
Hi,

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? 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<Av(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.
Apr 19 '07 #3
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

Apr 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Christian Christmann | last post by:
Hi, I've a header file with the declaration of an int vector: class BSet { private : std::vector<int> data;
2
by: slack_justyb | last post by:
Hello, I'm trying to figure the best way of doing the following. *I need an array of strings that are globally accessable from within 'arrayhere.h' *I need that array of strings to not change...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
3
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
6
by: zl2k | last post by:
I searched the topic and noticed that the initilization of static needs to be treated specially, but not know how. Here is my code which giving the link error: test.h...
22
by: silversurfer2025 | last post by:
Hello everybdy, I am a little confused for the following reason: In my code I used a simple for-loop in order to initialize a 2D-array of floats to zero. Because of efficiency reasons, I changed...
2
by: zl2k | last post by:
hi, all Suppose I have the following header file: #include <vector> using namespace std; class Test{ public: ~Test(); Test();
16
by: JDT | last post by:
Hi, Can someone show me how to set any integer (or float) in an std::vector as zero in a way other than using a for loop? Can we apply memset() or ZeroMemory() to the vector? Thanks for your...
9
by: Baby Lion | last post by:
list<intCurve; vector<Curvea; a.reserve(10); then how should I initialize the list in the vector before a.push_back(123) ? what about list<int>Curve ; vector<Curve *a;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.