449,369 Members | 1,631 Online Need help? Post your question and get tips & solutions from a community of 449,369 IT Pros & Developers. It's quick & easy.

# STL 2D Vector Questions

 P: n/a 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 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[i] << endl; } iii. Declaring testtwo will declare five vectors, each of which can contain integers. std::vector< std::vector > 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.push_back(20); testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.size(); " - no of rows in column 1" << endl; // This should return 2 testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.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 Nov 9 '05 #1
3 Replies

 P: n/a Daniel J Watkins wrote: 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 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 vi; for (vi::size_type i=0; i > testtwo(5); The vector "testtwo" contains five elements, no more, no less, as you see below. Each of these elements is empty (each std::vector is default constructed). 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.push_back(20); testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.size(); " - no of rows in column 1" << endl; // This should return 2 testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.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 Nov 9 '05 #2

 P: n/a Daniel J Watkins wrote: 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 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[i] << endl; } iii. Declaring testtwo will declare five vectors, each of which can contain integers. std::vector< std::vector > 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.push_back(20); testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.size(); " - no of rows in column 1" << endl; cout << testtwo.size() << " - no of rows in column 0" << endl; // This should return 2 testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.size(); " - no of rows in column 1" << endl; cout << testtwo.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 Nov 9 '05 #3

 P: n/a On 2005-11-09, Daniel J Watkins wrote: III. Declaring testtwo will declare five vectors, each of which can contain integers. std::vector< std::vector > 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. IV. This will return the number of vector contained in the vector. // 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.push_back(20); testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.size(); " - no of rows in column 1" << endl; // This should return 2 testtwo.push_back(20); testtwo.push_back(20); cout << testtwo.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 before pushing any int into that vector. -- Neil Cerutti Nov 9 '05 #4

### This discussion thread is closed

Replies have been disabled for this discussion. 