Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to initialize std::vector?

Reply
Thread Tools

how to initialize std::vector?

 
 
JDT
Guest
Posts: n/a
 
      01-20-2007
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
 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-20-2007
JDT wrote:
> 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
--
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
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-20-2007
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
 
Reply With Quote
 
Jim Langston
Guest
Posts: n/a
 
      01-20-2007
"JDT" <> wrote in message
news:ZKfsh.68668$. net...
> 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.


 
Reply With Quote
 
Daniel T.
Guest
Posts: n/a
 
      01-20-2007
JDT <> wrote:

> 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 );
 
Reply With Quote
 
Ivan Novick
Guest
Posts: n/a
 
      01-20-2007
Victor Bazarov wrote:
> JDT wrote:
>> 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.
>

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.

--
Ivan
http://www.0x4849.net
 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-20-2007
"JDT" <> wrote in message
news:ZKfsh.68668$. net
> 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);


--
John Carson


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-20-2007
"Ivan Novick" <> wrote in message
news:Quish.36248$ et
> Victor Bazarov wrote:
>> JDT wrote:
>>> 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.
>>

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


--
John Carson


 
Reply With Quote
 
John Carson
Guest
Posts: n/a
 
      01-20-2007
"John Carson" <jcarson_n_o_sp_am_@netspace.net.au> wrote in message
news:45b1f4aa$0$11211$
> "JDT" <> wrote in message
> news:ZKfsh.68668$. net
>> 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);


vector<int> v(100);

will also do it, but the OP may not want initialization.


--
John Carson


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-20-2007
Ivan Novick wrote:
> Victor Bazarov wrote:
>> JDT wrote:
>>> 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.
>>

> 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
--
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
 
 
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
How do you initialize signals in VHDL? Madhu VHDL 7 05-28-2008 06:43 PM
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
Initialize array using file i/o procedure/function? Brandon VHDL 5 09-29-2005 05:32 PM
initialize memory units Daniel Koethe VHDL 1 11-08-2004 08:36 PM



Advertisments