What is wrong with this?

M

mlt

I get an error out of range with the below code:

std::vector<std::vector<int>> vec;
vec[0][0] = 34;

why?
 
S

Sana

I get an error out of range with the below code:

std::vector<std::vector<int>> vec;
vec[0][0] = 34;

why?

Because there is no element there.

std::vector<std::vector<int> > vec;
std::vector<int> innerVector;
innerVector.push_back(34);
vec.push_back(innerVector);

now you can do

vec[0][0] = 42;
 
R

Reetesh Mukul

I get an error out of range with the below code:

std::vector<std::vector<int>> vec;
vec[0][0] = 34;

why?
std::vector<std::vector<int>> vec; //wrong syntactically

Should be something like this,

const int m = 20;
const int n = 30;
std::vector<std::vector<int> > vec(m); //correct syntactically
vec[0].resize(n);
vec[0][0] = 34;

With Regards,
Reetesh Mukul
 
M

mlt

This seems to solve the problem:

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


Victor Bazarov said:
mlt said:
I get an error out of range with the below code:

std::vector<std::vector<int>> vec;

What's the size of this vector? How many elements does it contain?
vec[0][0] = 34;

why?

Because you're out of range...

V
 
J

James Kanze

This seems to solve the problem:
std::vector<std:.vector<int>> vec(1)(1);

Are you sure it wasn't:
std::vector<std:.vector<int> > vec(1, 1) ;

The line you've posted shouldn't compile (and I can't imagine
what it should mean if it did).

It's also worth noting that there's a little bit of uncertainty
about the legality of the line I just wrote. Logically, it
should be:
std::vector< std::vector< int > > vec( 1,
std::vector< int >( 1 ) ) ;
There is special wording in the standard, however, to make
things like:
std::vector< int > vec( 10, 5 ) ;
work---the constructor that gets chosen here is an instantiation
of:
template< typename InputIterator >
vector< int >::vector( InputIterator begin, InputIterator end ) ;
while the one we (obviously want) is:
vector< int >::vector( size_t n, int init ) ;
(This second one requires a conversion of int to size_t, where
as the instantiation of the first is an exact match.) So the
standard has some special wording to make this constructor do
the right thing. Whether intended or not, it also makes
std::vector<std:.vector<int> > vec(1, 1) ;
work. (There was a defect report about this, but I don't know
what the final decision was.)

All of which probably isn't that important (unless the final
decision was to render my first suggestion illegal). What's
important is to realize that this idiom doesn't extend. You
can't write:

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

You have to do it right:
std::vector< std::vector< std::vector< int > > >
vec(
1, std::vector< std::vector< int > >(
1, std::vector< int >( 1 ) ) ) ;
(Obviously, a few typedefs will make the declaration a lot
simpler.)
 
B

Bo Persson

Reetesh said:
I get an error out of range with the below code:

std::vector<std::vector<int>> vec;
vec[0][0] = 34;

why?
std::vector<std::vector<int>> vec; //wrong syntactically

std::vector<std::vector<int> > vec; //correct syntactically

Both versions will very soon be formally correct syntax. Some
compilers (like MSVC) have already implemented the change.



Bo Person
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top