how to initialize std::vector?

Discussion in 'C++' started by JDT, Jan 20, 2007.

  1. JDT

    JDT Guest

    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
    JDT, Jan 20, 2007
    #1
    1. Advertising

  2. 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
    Victor Bazarov, Jan 20, 2007
    #2
    1. Advertising

  3. * 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?
    Alf P. Steinbach, Jan 20, 2007
    #3
  4. JDT

    Jim Langston Guest

    "JDT" <> wrote in message
    news:ZKfsh.68668$...
    > 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.
    Jim Langston, Jan 20, 2007
    #4
  5. JDT

    Daniel T. Guest

    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 );
    Daniel T., Jan 20, 2007
    #5
  6. JDT

    Ivan Novick Guest

    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
    Ivan Novick, Jan 20, 2007
    #6
  7. JDT

    John Carson Guest

    "JDT" <> wrote in message
    news:ZKfsh.68668$
    > 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
    John Carson, Jan 20, 2007
    #7
  8. JDT

    John Carson Guest

    "Ivan Novick" <> wrote in message
    news:Quish.36248$
    > 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
    John Carson, Jan 20, 2007
    #8
  9. JDT

    John Carson Guest

    "John Carson" <> wrote in message
    news:45b1f4aa$0$11211$
    > "JDT" <> wrote in message
    > news:ZKfsh.68668$
    >> 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
    John Carson, Jan 20, 2007
    #9
  10. 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
    Victor Bazarov, Jan 20, 2007
    #10
  11. Victor Bazarov wrote:
    > 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.
    Jacek Dziedzic, Jan 20, 2007
    #11
  12. JDT

    Ivan Novick Guest

    Victor Bazarov wrote:
    > 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".

    The subject of the message is how to initialize.

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

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

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

    --
    Ivan
    http://www.0x4849.net
    Ivan Novick, Jan 20, 2007
    #12
  13. Jacek Dziedzic wrote:
    > Victor Bazarov wrote:
    >> 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.
    Jacek Dziedzic, Jan 21, 2007
    #13
  14. Jacek Dziedzic wrote:
    > Jacek Dziedzic wrote:
    >> Victor Bazarov wrote:
    >>> 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jan 22, 2007
    #14
  15. JDT

    peter koch Guest

    Jim Langston skrev:
    > "JDT" <> wrote in message
    > news:ZKfsh.68668$...
    > > 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
    peter koch, Jan 22, 2007
    #15
  16. JDT

    Kai-Uwe Bux Guest

    peter koch wrote:

    >
    > Jim Langston skrev:
    >> "JDT" <> wrote in message
    >> news:ZKfsh.68668$...
    >> > 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
    Kai-Uwe Bux, Jan 22, 2007
    #16
  17. JDT

    Noah Roberts Guest

    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.


    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.
    Noah Roberts, Jan 22, 2007
    #17
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    20
    Views:
    4,288
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    718
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    1,913
    Csaba
    Feb 18, 2006
  4. Replies:
    10
    Views:
    774
    Jerry Coffin
    Jan 18, 2008
  5. Rune Allnor
    Replies:
    4
    Views:
    940
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page