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