STL 2D Vector Questions

  • Thread starter Daniel J Watkins
  • Start date
D

Daniel J Watkins

Hi,

Some runtime memory exceptions are being exhibited with some code I've
written. Can you clarify the following with you to see if my understanding
of the principles under question are correct. I'm trying to create a data
structure that will allow me to have a dynamic number of columns and a
dynamic number of rows in each column (there may be ten rows in column one
and twenty rows in column two for example).

i. I can declare 'testone' and use 'push_back' to insert values into the
vector and the vectpr will automatically be resized.

std::vector<int> testone;
testone.push_back(10);
testone.push_back(20);
testone.push_back(30);
testone.push_back(40);

ii. If I wanted to loop through 'testone' I could use the 'size' function to
determine the number of entries in the vector. Just as long as I don't index
the vector with a value larger than that returned by 'size' I won't be
accessing out of bounds memory.

for(int i = 0; i < testone.size(); i++)
{
cout << "Entry no[" << i << "]: " << testone << endl;
}

iii. Declaring testtwo will declare five vectors, each of which can contain
integers.

std::vector< std::vector<int> > testtwo(5);

iv. This will return the number of columns (vectors) declared.

// This should return 5
cout << testtwo.size() << " - no of columns " << endl;

v. I can index the vector at elements 0 and 1 as these elements are within
the original 5 element definition. I can expand the vector contained within
the column using the push_back and not worry about out of bounds errors.

// This should return 3
testtwo[0].push_back(20);
testtwo[0].push_back(20);
testtwo[0].push_back(20);
cout << testtwo[0].size(); " - no of rows in column 1" << endl;

// This should return 2
testtwo[1].push_back(20);
testtwo[1].push_back(20);
cout << testtwo[1].size(); " - no of rows in column 1" << endl;

vi. With regards to expanding the number of columns in the 2d vector, could
I use the following:

testtwo.resize( testtwo.size() + 1 );

There must be something wrong with my understanding of this though, as I
still get a memory exception based upon the above snippets.

Regards,

Daniel
 
J

Jonathan Mcdougall

Daniel said:
Hi,

Some runtime memory exceptions are being exhibited with some code I've
written. Can you clarify the following with you to see if my understanding
of the principles under question are correct. I'm trying to create a data
structure that will allow me to have a dynamic number of columns and a
dynamic number of rows in each column (there may be ten rows in column one
and twenty rows in column two for example).

i. I can declare 'testone' and use 'push_back' to insert values into the
vector and the vectpr will automatically be resized.

std::vector<int> testone;
testone.push_back(10);
testone.push_back(20);
testone.push_back(30);
testone.push_back(40);
Ok.

ii. If I wanted to loop through 'testone' I could use the 'size' function to
determine the number of entries in the vector. Just as long as I don't index
the vector with a value larger than that returned by 'size' I won't be
accessing out of bounds memory.

for(int i = 0; i < testone.size(); i++)

Is there a reason why you don't use the type returned by a function?
std::vector::size returns std::vector::size_type.

typedef std::vector<int> vi;
for (vi::size_type i=0; i<testone.size(); ++i)

Iterators could also do the job here:

for (vi::iterator itor=testone.begin(); itor!=testone.end(); ++itor)
std::cout << *itor;

Also, see
http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.15.
{
cout << "Entry no[" << i << "]: " << testone << endl;
}

iii. Declaring testtwo will declare five vectors, each of which can contain
integers.


Be careful with words. This does not "declare" five vectors, it defines
only one having 5 elements.
std::vector< std::vector<int> > testtwo(5);

The vector "testtwo" contains five elements, no more, no less, as you
iv. This will return the number of columns (vectors) declared.

// This should return 5
cout << testtwo.size() << " - no of columns " << endl;

Yes it does.
v. I can index the vector at elements 0 and 1 as these elements are within
the original 5 element definition.
Yes.

I can expand the vector contained within
the column using the push_back and not worry about out of bounds errors.

That's the point of having container classes, yes.
// This should return 3
testtwo[0].push_back(20);
testtwo[0].push_back(20);
testtwo[0].push_back(20);
cout << testtwo[0].size(); " - no of rows in column 1" << endl;

// This should return 2
testtwo[1].push_back(20);
testtwo[1].push_back(20);
cout << testtwo[1].size(); " - no of rows in column 1" << endl;

Yes (it's actually column 2, but whatever. What would we do without
copy/paste).
vi. With regards to expanding the number of columns in the 2d vector, could
I use the following:

testtwo.resize( testtwo.size() + 1 );

Yes. That would increase the size of testtwo by one. It would contain
exactly 6 elements.
There must be something wrong with my understanding of this though, as I
still get a memory exception based upon the above snippets.

Line 42 perhaps?


Jonathan
 
M

mlimber

Daniel said:
Hi,

Some runtime memory exceptions are being exhibited with some code I've
written. Can you clarify the following with you to see if my understanding
of the principles under question are correct. I'm trying to create a data
structure that will allow me to have a dynamic number of columns and a
dynamic number of rows in each column (there may be ten rows in column one
and twenty rows in column two for example).

i. I can declare 'testone' and use 'push_back' to insert values into the
vector and the vectpr will automatically be resized.

std::vector<int> testone;
testone.push_back(10);
testone.push_back(20);
testone.push_back(30);
testone.push_back(40);

ii. If I wanted to loop through 'testone' I could use the 'size' function to
determine the number of entries in the vector. Just as long as I don't index
the vector with a value larger than that returned by 'size' I won't be
accessing out of bounds memory.

for(int i = 0; i < testone.size(); i++)
{
cout << "Entry no[" << i << "]: " << testone << endl;
}

iii. Declaring testtwo will declare five vectors, each of which can contain
integers.

std::vector< std::vector<int> > testtwo(5);

iv. This will return the number of columns (vectors) declared.

// This should return 5
cout << testtwo.size() << " - no of columns " << endl;

v. I can index the vector at elements 0 and 1 as these elements are within
the original 5 element definition. I can expand the vector contained within
the column using the push_back and not worry about out of bounds errors.

// This should return 3
testtwo[0].push_back(20);
testtwo[0].push_back(20);
testtwo[0].push_back(20);
cout << testtwo[0].size(); " - no of rows in column 1" << endl;


cout << testtwo[0].size() << " - no of rows in column 0" << endl;
// This should return 2
testtwo[1].push_back(20);
testtwo[1].push_back(20);
cout << testtwo[1].size(); " - no of rows in column 1" << endl;

cout << testtwo[1].size() << " - no of rows in column 1" << endl;
vi. With regards to expanding the number of columns in the 2d vector, could
I use the following:

testtwo.resize( testtwo.size() + 1 );

There must be something wrong with my understanding of this though, as I
still get a memory exception based upon the above snippets.

Under VC++ 6 (sp6 with dinkumware patches applied) and g++ 3.4.1, the
code works fine for me. Is there more to it than you're showing (e.g.
use of iterators)?
Regards,

Daniel

Cheers! --M
 
N

Neil Cerutti

III. Declaring testtwo will declare five vectors, each of which
can contain integers.

std::vector< std::vector<int> > testtwo(5);

You are confused about the difference between defining and
declaring. Check the C++FAQ for more info.

The above if a definition *and* a declaration. But not every
declaration is a definition.
IV. This will return the number of columns (vectors) declared.

// This should return 5
cout << testtwo.size() << " - no of columns " << endl;
Yes.

V. I can index the vector at elements 0 and 1 as these elements
are within the original 5 element definition. I can expand the
vector contained within the column using the push_back and not
worry about out of bounds errors.

// This should return 3
testtwo[0].push_back(20);
testtwo[0].push_back(20);
testtwo[0].push_back(20);
cout << testtwo[0].size(); " - no of rows in column 1" << endl;

// This should return 2
testtwo[1].push_back(20);
testtwo[1].push_back(20);
cout << testtwo[1].size(); " - no of rows in column 1" << endl;

vi. With regards to expanding the number of columns in the 2d
vector, could I use the following:

testtwo.resize( testtwo.size() + 1 );

There must be something wrong with my understanding of this
though, as I still get a memory exception based upon the above
snippets.

You'll need to show more of your real code, because your snippets
look fine.

Most likely you're doing something like referencing testtwo[2][0]
before pushing any int into that vector.
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top