mlt wrote:
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.