Correct usage of std::vector?

Discussion in 'C++' started by mlt, Feb 14, 2009.

  1. mlt

    mlt Guest

    I need to use a std::vector to specify a matrix of integers (my code must
    not use std::boost). But is:

    std::vector<std::vector<int>> vec(1,1);
    vec[0][0] = 34;

    an example of correct use?
     
    mlt, Feb 14, 2009
    #1
    1. Advertising

  2. mlt

    Kai-Uwe Bux Guest

    mlt wrote:

    > I need to use a std::vector to specify a matrix of integers (my code must
    > not use std::boost). But is:
    >
    > std::vector<std::vector<int>> vec(1,1);
    > vec[0][0] = 34;
    >
    > an example of correct use?


    No:

    a) std::vector<std::vector<int>> is currently a syntax error because of
    the ">>" token. Make that

    std::vector< std::vector< int > >

    b) The constructor arguments should read:

    std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );

    In general,

    std::vector< std::vector< int > >
    vec ( rows, std::vector< int >(cols) );

    should do what you want.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 14, 2009
    #2
    1. Advertising

  3. mlt

    mlt Guest

    "Kai-Uwe Bux" <> wrote in message
    news:gn65ko$ed3$...
    > mlt wrote:
    >
    >> I need to use a std::vector to specify a matrix of integers (my code must
    >> not use std::boost). But is:
    >>
    >> std::vector<std::vector<int>> vec(1,1);
    >> vec[0][0] = 34;
    >>
    >> an example of correct use?

    >
    > No:
    >
    > a) std::vector<std::vector<int>> is currently a syntax error because of
    > the ">>" token. Make that
    >
    > std::vector< std::vector< int > >
    >
    > b) The constructor arguments should read:
    >
    > std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    >
    > In general,
    >
    > std::vector< std::vector< int > >
    > vec ( rows, std::vector< int >(cols) );
    >
    > should do what you want.
    >
    >
    > Best
    >
    > Kai-Uwe Bux



    Ok I see you point but currently:

    std::vector<std::vector<int>> vec(1,1);

    does not give any compiler error (Using Visual Studio 2008) and when running
    the app I also get the correct behaviour. So I don't think its necessary to
    do:

    std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );

    when using Visual Studio at least (have not tested on unix yet).
     
    mlt, Feb 14, 2009
    #3
  4. mlt

    Kai-Uwe Bux Guest

    mlt wrote:

    >
    > "Kai-Uwe Bux" <> wrote in message
    > news:gn65ko$ed3$...
    >> mlt wrote:
    >>
    >>> I need to use a std::vector to specify a matrix of integers (my code
    >>> must not use std::boost). But is:
    >>>
    >>> std::vector<std::vector<int>> vec(1,1);
    >>> vec[0][0] = 34;
    >>>
    >>> an example of correct use?

    >>
    >> No:
    >>
    >> a) std::vector<std::vector<int>> is currently a syntax error because of
    >> the ">>" token. Make that
    >>
    >> std::vector< std::vector< int > >
    >>
    >> b) The constructor arguments should read:
    >>
    >> std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    >>
    >> In general,
    >>
    >> std::vector< std::vector< int > >
    >> vec ( rows, std::vector< int >(cols) );
    >>
    >> should do what you want.
    >>
    >>
    >> Best
    >>
    >> Kai-Uwe Bux

    >
    >
    > Ok I see you point but currently:
    >
    > std::vector<std::vector<int>> vec(1,1);
    >
    > does not give any compiler error (Using Visual Studio 2008) and when
    > running the app I also get the correct behaviour. So I don't think its
    > necessary to do:
    >
    > std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    >
    > when using Visual Studio at least (have not tested on unix yet).


    Well, it does not work with g++.

    Presumably, whether you get an error from

    std::vector<std::vector<int>> vec(1,1);

    will depend on the STL that your vendor uses. The constructor that gets
    invoked it

    std::vector<T> ( size_type n, T const & t )

    for T = std::vector<int>. Now, since std::vector<int> in turn can be
    initialized from a size_type, a conversion of the argument can take place.
    I am not sure whether g++ is at fault for not doing that: the constructor
    needed for the conversion is marked as "explicit" in the standard.
    Therefore, I think, no automatic conversion should take place.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 14, 2009
    #4
  5. mlt

    James Kanze Guest

    On Feb 14, 11:59 am, Kai-Uwe Bux <> wrote:
    > mlt wrote:


    > > "Kai-Uwe Bux" <> wrote in message
    > >news:gn65ko$ed3$...
    > >> mlt wrote:


    > >>> I need to use a std::vector to specify a matrix of
    > >>> integers (my code must not use std::boost). But is:


    > >>> std::vector<std::vector<int>> vec(1,1);
    > >>> vec[0][0] = 34;


    > >>> an example of correct use?


    > >> No:


    > >> a) std::vector<std::vector<int>> is currently a syntax
    > >> error because of the ">>" token. Make that


    > >> std::vector< std::vector< int > >


    > >> b) The constructor arguments should read:


    > >> std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );


    > >> In general,


    > >> std::vector< std::vector< int > >
    > >> vec ( rows, std::vector< int >(cols) );


    > >> should do what you want.


    > > Ok I see you point but currently:


    > > std::vector<std::vector<int>> vec(1,1);


    > > does not give any compiler error (Using Visual Studio 2008)
    > > and when running the app I also get the correct behaviour.
    > > So I don't think its necessary to do:


    > > std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );


    > > when using Visual Studio at least (have not tested on unix yet).


    > Well, it does not work with g++.


    > Presumably, whether you get an error from


    > std::vector<std::vector<int>> vec(1,1);


    > will depend on the STL that your vendor uses.


    Formally, the standard requires it to work. Whether this is
    intentional or not is another question---it may be an error in
    the standard. (On the other hand: the standard has guaranteed
    it, so it's rather hard to say that in fact, it is illegal, even
    if the guarantee wasn't intentional.)

    > The constructor that gets invoked it


    > std::vector<T> ( size_type n, T const & t )


    No. The constructor which gets invoked is an instantiation of:

    template< InputIterator >
    vector::vector( InputIterator begin, InputIterator end ) ;

    Instantiated with InputIterator == int, this is an exact match;
    the one you propose requires a conversion of int to size_t.

    > for T = std::vector<int>. Now, since std::vector<int> in turn
    > can be initialized from a size_type, a conversion of the
    > argument can take place. I am not sure whether g++ is at
    > fault for not doing that: the constructor needed for the
    > conversion is marked as "explicit" in the standard.
    > Therefore, I think, no automatic conversion should take place.


    The standard says (§23.1.1/9) that if in the above template
    constructor, InputIterator has an integral type (and it does
    here), it shall have the same effect as:

    std::vector( static_cast< size_type >( begin ),
    static_cast< value_type >( end ) )

    Those static_cast are explicit conversions.

    This may not be what was desired, but it is definitly what the
    current standard requires (and a quick glance at the CD shows
    no changes here).

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Feb 14, 2009
    #5
  6. mlt

    Guest

    On Feb 14, 3:44 pm, James Kanze <> wrote:
    > On Feb 14, 11:59 am, Kai-Uwe Bux <> wrote:
    >
    >
    >
    >
    >
    > > mlt wrote:
    > > > "Kai-Uwe Bux" <> wrote in message
    > > >news:gn65ko$ed3$...
    > > >> mlt wrote:
    > > >>> I need to use a std::vector to specify a matrix of
    > > >>> integers (my code must not use std::boost). But is:
    > > >>>    std::vector<std::vector<int>> vec(1,1);
    > > >>>    vec[0][0] = 34;
    > > >>> an example of correct use?
    > > >> No:
    > > >> a) std::vector<std::vector<int>> is currently a syntax
    > > >> error because of the ">>" token. Make that
    > > >>  std::vector< std::vector< int > >
    > > >> b) The constructor arguments should read:
    > > >>  std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    > > >> In general,
    > > >>  std::vector< std::vector< int > >
    > > >>    vec ( rows, std::vector< int >(cols) );
    > > >> should do what you want.
    > > > Ok I see you point but currently:
    > > >    std::vector<std::vector<int>> vec(1,1);
    > > > does not give any compiler error (Using Visual Studio 2008)
    > > > and when running the app I also get the correct behaviour.
    > > > So I don't think its necessary to do:
    > > >     std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    > > > when using Visual Studio at least (have not tested on unix yet).

    > > Well, it does not work with g++.
    > > Presumably, whether you get an error from
    > >   std::vector<std::vector<int>> vec(1,1);
    > > will depend on the STL that your vendor uses.

    >
    > Formally, the standard requires it to work.  Whether this is
    > intentional or not is another question---it may be an error in
    > the standard.  (On the other hand: the standard has guaranteed
    > it, so it's rather hard to say that in fact, it is illegal, even
    > if the guarantee wasn't intentional.)
    >
    > > The constructor that gets invoked it
    > >   std::vector<T> ( size_type n, T const & t )

    >
    > No.  The constructor which gets invoked is an instantiation of:
    >
    >     template< InputIterator >
    >     vector::vector( InputIterator begin, InputIterator end ) ;
    >
    > Instantiated with InputIterator == int, this is an exact match;
    > the one you propose requires a conversion of int to size_t.
    >


    This is a good example of why C++ compilers should have an option to
    show what they're generating for an instantiation. Both of you are
    experienced and intelligent and yet at least one of you is wrong.
    Open code generation is better for a number of reasons.


    Brian Wood
    Ebenezer Enterprises
    www.webEbenezer.net

    I recommend the articles on social topics by this physics professor --
    http://wuphys.wustl.edu/~katz/
     
    , Feb 14, 2009
    #6
  7. mlt

    Kai-Uwe Bux Guest

    wrote:

    > On Feb 14, 3:44 pm, James Kanze <> wrote:
    >> On Feb 14, 11:59 am, Kai-Uwe Bux <> wrote:
    >>
    >>
    >>
    >>
    >>
    >> > mlt wrote:
    >> > > "Kai-Uwe Bux" <> wrote in message
    >> > >news:gn65ko$ed3$...
    >> > >> mlt wrote:
    >> > >>> I need to use a std::vector to specify a matrix of
    >> > >>> integers (my code must not use std::boost). But is:
    >> > >>> std::vector<std::vector<int>> vec(1,1);
    >> > >>> vec[0][0] = 34;
    >> > >>> an example of correct use?
    >> > >> No:
    >> > >> a) std::vector<std::vector<int>> is currently a syntax
    >> > >> error because of the ">>" token. Make that
    >> > >> std::vector< std::vector< int > >
    >> > >> b) The constructor arguments should read:
    >> > >> std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    >> > >> In general,
    >> > >> std::vector< std::vector< int > >
    >> > >> vec ( rows, std::vector< int >(cols) );
    >> > >> should do what you want.
    >> > > Ok I see you point but currently:
    >> > > std::vector<std::vector<int>> vec(1,1);
    >> > > does not give any compiler error (Using Visual Studio 2008)
    >> > > and when running the app I also get the correct behaviour.
    >> > > So I don't think its necessary to do:
    >> > > std::vector< std::vector< int > > vec ( 1, std::vector< int >(1) );
    >> > > when using Visual Studio at least (have not tested on unix yet).
    >> > Well, it does not work with g++.
    >> > Presumably, whether you get an error from
    >> > std::vector<std::vector<int>> vec(1,1);
    >> > will depend on the STL that your vendor uses.

    >>
    >> Formally, the standard requires it to work.  Whether this is
    >> intentional or not is another question---it may be an error in
    >> the standard.  (On the other hand: the standard has guaranteed
    >> it, so it's rather hard to say that in fact, it is illegal, even
    >> if the guarantee wasn't intentional.)
    >>
    >> > The constructor that gets invoked it
    >> > std::vector<T> ( size_type n, T const & t )

    >>
    >> No.  The constructor which gets invoked is an instantiation of:
    >>
    >> template< InputIterator >
    >> vector::vector( InputIterator begin, InputIterator end ) ;
    >>
    >> Instantiated with InputIterator == int, this is an exact match;
    >> the one you propose requires a conversion of int to size_t.
    >>

    >
    > This is a good example of why C++ compilers should have an option to
    > show what they're generating for an instantiation. Both of you are
    > experienced and intelligent and yet at least one of you is wrong.


    The wrong one would be me. I re-checked the standard, and James is
    absolutely correct. I missed that part (in fact, I missed it earlier, too;
    and now I have to fix my vector implementation).

    > Open code generation is better for a number of reasons.


    What is open code generation?


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Feb 15, 2009
    #7
  8. mlt

    red floyd Guest

    mlt wrote:
    > I need to use a std::vector to specify a matrix of integers (my code
    > must not use std::boost). But is:
    >


    Just to be pedantic, nobody's code may use std::boost. I don't believe
    that ISO/IEC 14882:2003 defines such an entity.
     
    red floyd, Feb 15, 2009
    #8
  9. mlt

    Guest

    On Feb 14, 6:07 pm, Kai-Uwe Bux <> wrote:
    > wrote:
    > > Open code generation is better for a number of reasons.

    >
    > What is open code generation?
    >


    Code generation where the results can be reviewed by a person.
    Some C++ compilers have an option that lets you inspect the
    results of preprocessing. In that sense I would call them
    open code generators. The same is not true with templates.
    I've heard suggestions for making it easier to trace the
    steps a compiler takes when it instantiates something, but I
    don't know of any compilers that have that either.

    Besides making it easier for people to understand what the
    compiler has done, I think fully instantiated source code is
    important for on line code generation. It would be the
    portable output of an on line code generator rather than
    attempting to generate object code for a long list of
    hardware and operating systems.


    Brian Wood
    Ebenezer Enterprises
    www.webEbenezer.net
     
    , Feb 15, 2009
    #9
  10. mlt

    James Kanze Guest

    On Feb 15, 1:07 am, Kai-Uwe Bux <> wrote:
    > wrote:

    [...]
    > >> Formally, the standard requires it to work. Whether this is
    > >> intentional or not is another question---it may be an error in
    > >> the standard. (On the other hand: the standard has guaranteed
    > >> it, so it's rather hard to say that in fact, it is illegal, even
    > >> if the guarantee wasn't intentional.)


    > >> > The constructor that gets invoked it
    > >> > std::vector<T> ( size_type n, T const & t )


    > >> No. The constructor which gets invoked is an instantiation of:


    > >> template< InputIterator >
    > >> vector::vector( InputIterator begin, InputIterator end ) ;


    > >> Instantiated with InputIterator == int, this is an exact
    > >> match; the one you propose requires a conversion of int to
    > >> size_t.


    > > This is a good example of why C++ compilers should have an
    > > option to show what they're generating for an instantiation.
    > > Both of you are experienced and intelligent and yet at least
    > > one of you is wrong.


    > The wrong one would be me. I re-checked the standard, and
    > James is absolutely correct. I missed that part (in fact, I
    > missed it earlier, too; and now I have to fix my vector
    > implementation).


    It helps to have discussed the issues with someone who has
    actually implemented the library. It certainly wouldn't have
    occured to me on my own.

    An obvious test case for the library is something like:
    std::vector< int > v( 5, 10 ) ;
    Without the special casing, this fails (which tends to surprise
    at first). I think that in some of the initial, pre-standard
    implementations of the library, it also failed; that you had to
    write:
    std::vector< int > v( static_cast< size_t >( 5 ), 10 ) ;
    .. The committee felt that this would be too embarassing, and so
    added the special case.

    --
    James Kanze (GABI Software) email:
    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
     
    James Kanze, Feb 15, 2009
    #10
    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,434
    Pete Becker
    Mar 30, 2005
  2. Jason Heyes
    Replies:
    8
    Views:
    768
    Andrew Koenig
    Jan 15, 2006
  3. Replies:
    8
    Views:
    2,001
    Csaba
    Feb 18, 2006
  4. Replies:
    7
    Views:
    2,595
    Hendrik Schober
    Oct 1, 2008
  5. Rune Allnor
    Replies:
    4
    Views:
    993
    Rune Allnor
    Dec 11, 2008
Loading...

Share This Page