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[i] << 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 3 6708
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<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-lit...html#faq-13.15.
{ cout << "Entry no[" << i << "]: " << testone[i] << 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
see below. Each of these elements is empty (each std::vector<int> 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[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
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<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[i] << 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
On 2005-11-09, Daniel J Watkins <da****************@hotmail.com>
wrote: 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.
IV. This will return the number of vector<int> 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[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.
--
Neil Cerutti This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: William Payne |
last post by:
(This post is related to "recent files menu"-post below. If I should have
kept this in that thread, I apologise.)
Hello, I have a function that adds a std::string to a std::vector. New
entries are...
|
by: Venn Syii |
last post by:
I've searched all the forums but cannot find an answer to this question.
I do the following:
vector<MyClass*> myClassList;
Later in the program I try to add to myClassList with a...
|
by: pmatos |
last post by:
Hi all,
I have a vector of vector of ints, I could use C approach by using
int but I think C++ vector<vector<int> > would be easier to manage.
So I have a function which creates and initializes...
|
by: Alfonso Morra |
last post by:
I have the ff code for testing the concept of storing objects:
#include <vector>
#include <iostream>
using namespace std ;
class MyClass {
public:
MyClass(){
|
by: Michael Sgier |
last post by:
Hello
If someone could explain the code below to me would be great.
// return angle between two vectors
const float inline Angle(const CVector& normal) const
{
return acosf(*this % normal);
}...
|
by: lokchan |
last post by:
i want to create a vector of pointer s.t. it can handle new and delete
but also have std::vector interface
can i implement by partial specialization and inherence like follow ?
#include...
|
by: madhu |
last post by:
vector<vector<vector<long Vector3D; // 3dvector.
for (long k = 0; j < Depth; j++ )
{
Vector3D.push_back ( vector<vector<A_Type() );
for (long j = 0; j < Height; j++ )
{
Vector3D.push_back (...
|
by: Jess |
last post by:
Hello,
I tried to clear a vector "v" using "v.clear()". If "v" contains
those objects that are non-built-in (e.g. string), then "clear()" can
indeed remove all contents. However, if "v"...
|
by: Andy |
last post by:
Hi all,
I started developing a little app on my Mac using XCode some month
ago. The app is running fine on my mac like a sharm. Now I am nearly
ready and yesterday I moved the whole source code...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |