how to initialize std::vector?

J

JDT

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
 
V

Victor Bazarov

JDT said:
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?

No.

vector<float> myvector;
... // fill myvector with something

vector<float>(myvector.size(), 0f).swap(myvector);

V
 
A

Alf P. Steinbach

* JDT:
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?

Note that any way you do it, there will be a loop, at some level of the
implementation.

An easy but perhaps not the most efficient way for a vector v of type V is

V( v.size() ).swap( v );

Or you can do e.g.

std::fill( v.begin(), v.end(), 0 );



Can we apply memset() or
ZeroMemory() to the vector?

std::memset can be applied to a vector of POD element type, but it's not
recommended, especially not for one who is in doubt about its usage.

ZeroMemory is not a standard C++ function.
 
J

Jim Langston

JDT said:
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

You *could* use memset, but personally I don't like it. The reason being it
can break classes.

Say, for instance you have a std::vector of a simple POD class such as:

class MyClass
{
public:
int MyInt;
float MyFloat;
char MyChar;
};

std::vector<MyClass> MyVector;

So you go ahead and use memset something like (this may be off):

memset( &MyVector[0], 0, sizeof MyClass * MyVector.count() );

Okay, it works for now. But maybe later you change MyClass to also include
something that's not POD.

class MyClass
{
public:
int MyInt;
float MyFloat;
char MyChar;
std::string MyString;
};

the memset will still set the variables to 0, but now you just broke
MyString. MyString has a constructor that sets all kinds of things such as
pointers that aren't necessarily 0.

Consider alternatives.
 
D

Daniel T.

JDT said:
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.

You could just assign 0 to the element you want to set. If you meant
"every" instead of "any". There are several ways:

a) The vector/swap trick that Victor & Alf showed.
b) The std::fill algorithm that Alf showed.

You could also:

unsigned s = vec.size();
vec.clear();
vec.resize( s );
 
I

Ivan Novick

Victor said:
Hmmm.... the question says how to initialize the vector. So would not
this satisfy the question:

std::vector<int> v;
v.resize(100);

Thus creating 100 elements that are all default initialized, or 0
initialized in the case of ints.
 
J

John Carson

JDT said:
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


For a vector with 100 zeros:

vector<int> v(100,0);
 
J

John Carson

Ivan Novick said:
Hmmm.... the question says how to initialize the vector. So would not
this satisfy the question:

std::vector<int> v;
v.resize(100);

Thus creating 100 elements that are all default initialized, or 0
initialized in the case of ints.

If it's initialization, then

std::vector<int> v(100);

will do it without a resize.
 
V

Victor Bazarov

Ivan said:
Hmmm.... the question says how to initialize the vector.

No, it doesn't. It asks how to "set any integer".
So would not
this satisfy the question:

std::vector<int> v;
v.resize(100);

Thus creating 100 elements that are all default initialized, or 0
initialized in the case of ints.

Just to explain to those who missed it, the "no" is for the last
question (about 'memset' or 'ZeroMemory' application to a vector).
I hope it's clearer now.

My answer to how to set the contents of the vector to 0 was given
in the same message and for whatever reason snipped away by Ivan.

V
 
J

Jacek Dziedzic

Victor said:
Just to explain to those who missed it, the "no" is for the last
question (about 'memset' or 'ZeroMemory' application to a vector).
I hope it's clearer now.

My answer to how to set the contents of the vector to 0 was given
in the same message and for whatever reason snipped away by Ivan.

V

Strictly speaking, I believe it is (technically) legal
to use memset() starting at &(vector_of_int[0]) to set
the values of the vector's elements to some values
(admittedly, those who's representation consists of equal bytes).

So it would be (technically) legal to zero-initialize a
vector of ints like that, no?

Though I'd bet the default-initialization does exactly
that.

- J.
 
I

Ivan Novick

Victor said:
No, it doesn't. It asks how to "set any integer".
The subject of the message is how to initialize.

In which case I would say John Carson's reply is the best:

std::vector said:
Just to explain to those who missed it, the "no" is for the last
question (about 'memset' or 'ZeroMemory' application to a vector).
I hope it's clearer now.

My answer to how to set the contents of the vector to 0 was given
in the same message and for whatever reason snipped away by Ivan.
Woops, overzealous snipping on my part. Sorry.
 
J

Jacek Dziedzic

Jacek said:
Victor said:
Just to explain to those who missed it, the "no" is for the last
question (about 'memset' or 'ZeroMemory' application to a vector).
I hope it's clearer now.

My answer to how to set the contents of the vector to 0 was given
in the same message and for whatever reason snipped away by Ivan.

V

Strictly speaking, I believe it is (technically) legal
to use memset() starting at &(vector_of_int[0]) to set
the values of the vector's elements to some values
(admittedly, those who's representation consists of equal bytes).

So it would be (technically) legal to zero-initialize a
vector of ints like that, no?

Though I'd bet the default-initialization does exactly
that.

Just to clarify -- I obviously meant the case where the
vector is large enough (resized appropriately) so that the
memset does not trash what comes after the vector.

- J.
 
V

Victor Bazarov

Jacek said:
Jacek said:
Victor said:
Just to explain to those who missed it, the "no" is for the last
question (about 'memset' or 'ZeroMemory' application to a vector).
I hope it's clearer now.

My answer to how to set the contents of the vector to 0 was given
in the same message and for whatever reason snipped away by Ivan.

V

Strictly speaking, I believe it is (technically) legal
to use memset() starting at &(vector_of_int[0]) to set
the values of the vector's elements to some values
(admittedly, those who's representation consists of equal bytes).

So it would be (technically) legal to zero-initialize a
vector of ints like that, no?

Though I'd bet the default-initialization does exactly
that.

Just to clarify -- I obviously meant the case where the
vector is large enough (resized appropriately) so that the
memset does not trash what comes after the vector.

I wouldn't bet the default initialisation does that, but it is most
likely safe to do with vectors of POD.

V
 
P

peter koch

Jim Langston skrev:
JDT said:
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

You *could* use memset, but personally I don't like it. The reason being it
can break classes.

Say, for instance you have a std::vector of a simple POD class such as:

class MyClass
{
public:
int MyInt;
float MyFloat;
char MyChar;
};

std::vector<MyClass> MyVector;

So you go ahead and use memset something like (this may be off):

memset( &MyVector[0], 0, sizeof MyClass * MyVector.count() );

Okay, it works for now.
[snip]

That depends on what you mean when it works. You can't expect MyFloat
to be zero - although it likely is on your platform, and I am not even
sure that you can expect MyInt or MyChar to be zero. (I do not believe
a number format where "all zeros" means lowest possible value is
nonconforming).

/Peter
 
K

Kai-Uwe Bux

peter said:
Jim Langston skrev:
JDT said:
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

You *could* use memset, but personally I don't like it. The reason being
it can break classes.

Say, for instance you have a std::vector of a simple POD class such as:

class MyClass
{
public:
int MyInt;
float MyFloat;
char MyChar;
};

std::vector<MyClass> MyVector;

So you go ahead and use memset something like (this may be off):

memset( &MyVector[0], 0, sizeof MyClass * MyVector.count() );

Okay, it works for now.
[snip]

That depends on what you mean when it works. You can't expect MyFloat
to be zero - although it likely is on your platform, and I am not even
sure that you can expect MyInt or MyChar to be zero. (I do not believe
a number format where "all zeros" means lowest possible value is
nonconforming).

From the standard

[3.9.1/7]

[...] The representations of integral types shall define values by use of
a pure binary numeration system.44) [...]

[44] A positional representation for integers that uses the binary digits
0 and 1, in which the values represented by successive bits are additive,
begin with 1, and are multiplied by successive integral power of 2, except
perhaps for the bit with the highest position.
(Adapted from the American National Dictionary for Information Processing
Systems.)

[3.9.1/8]

[...] The value representation of floating-point types is
implementation-defined. [...]


Best

Kai-Uwe Bux
 
N

Noah Roberts

Victor said:

Actually, you can. It bypasses much of the safety of using a vector
but with care it can be done.

std::vector<int> x;
x.resize(100);

memset(&v[0], 0, sizeof(int) * 100);

Of course, there isn't much reason to do this (since resize() already
did it in a more well defined manner), but it *can* be done. There are
many other times when such techniques are reasonable even if rather
dangerous. Any time you need a buffer of some type a std::vector can
be used in place; you can pass &v[0] and know that, assuming you've
told the vector to allocate enough room, it will do what you expect.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top