473,805 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

STL 2D Vector Questions

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_ba ck(10);
testone.push_ba ck(20);
testone.push_ba ck(30);
testone.push_ba ck(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
Nov 9 '05 #1
3 6736
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_ba ck(10);
testone.push_ba ck(20);
testone.push_ba ck(30);
testone.push_ba ck(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::si ze returns std::vector::si ze_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.be gin(); itor!=testone.e nd(); ++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

Nov 9 '05 #2
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_ba ck(10);
testone.push_ba ck(20);
testone.push_ba ck(30);
testone.push_ba ck(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

Nov 9 '05 #3
On 2005-11-09, Daniel J Watkins <da************ ****@hotmail.co m>
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
Nov 9 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
12642
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 added at the front (index 0) of the vector. If the vector contains a certain amount of elements, the element at the back is removed when a new one is added. If one tries to add a string already stored in the vector, that string is supposed to...
4
3102
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 .push_back(...) I get an "out of memory" runtime error. I know I'm not out of memory because normal vectors such as vector<int> a, still work, and still work fine.
5
10582
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 the vector with the values I need (I know these values before hand). - What's the best way to initialize the vector<vector<int> >? Can I initilize it by enumerating its values? - If I do: v = new vector<vector<int> >(3) for example, is it
12
3958
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(){
14
4005
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); } // reflect this vector off surface with normal vector
6
3267
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 <vector> #include <algorithm> template <typename T> struct delete_ptr {
6
2364
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 ( vector<A_Type>() ); for ( long i = 0; i < Width; i++ ) {
9
3763
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" contains built-in types (e.g. int), then "clear()" doesn't remove anything at all. Why does "clear()" have this behaviour? Also, when I copy one vector "v1" from another vector "v2", with "v1" longer than "v2" (e.g. "v1" has 2 elements and "v2" has...
6
11093
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 to a Windows PC and build it in MS VC++. Now I got a lot of "vector subscript out of range" assertion errors and I don't know why because on my Mac it is working without any error. What does the Mac with this problems and what is the magic behind...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10613
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10363
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10368
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10107
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9186
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6876
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4327
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.