473,804 Members | 3,038 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.siz e;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 2057
pe*********@gma il.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.siz e;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.siz e(), A(false)).swap( v);

or

v = vector<A>(v.siz e(), 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************ *********@d57g2 000hsg.googlegr oups.com>,
pe*********@gma il.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.siz e;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...@gma il.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.siz e;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 objektorientier ter Datenverarbeitu ng
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
7320
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
57997
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 once initialized 'const' *I would like to initialize said array with a one liner, but if not something extremly easy to read. *I need to know how many elements there are in the array so I can stop
5
4578
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 know you wouldn't use 'C' { private: static std::vector<int> PSArray; public:
3
5941
by: jut_bit_zx | last post by:
class A { public: A(); virtual ~A(){} .... private: int m_iarray; }
6
23586
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 ------------------------------------- #ifndef TEST_H #define TEST_H #include <vector> #include <iostream>
22
26749
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 it to use memset and I get totally different results.. How can this be? Here is the example: float gaborfilter;
2
21345
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
26035
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 help. JD
9
2786
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
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10571
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5520
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.