473,403 Members | 2,323 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

2-dimensional vector (using STL)

Dear all,

I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

Can someone point me in a direction to solve this problem? The source is
given below.

Many thanks,

Koen

/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;
public:
CFrame();
CFrame(int h,int w, int * buffer);
};

/* cpp file */

#include "CFrame.h"
#include <vector>
CFrame::CFrame(){
width = 0;
height = 0;
}
CFrame::CFrame(int width, int height, int * buffer){
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[i][j] = buffer [i * j + j];
}
}
Jul 22 '05 #1
4 2408
In article <bq**********@gaudi2.UGent.be>,
Koen De Wolf <Ko**@raadselweb.net> wrote:

I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;
The vector Y has zero size, which means that you cannot use the []
operator either to retrieve or store data. Since it appears you know in
advance how much data is going to go into the vector, you should set its
size in the constructor before putting data into it.
public:
CFrame();
CFrame(int h,int w, int * buffer);
}; [snip]

Set the size in the constructor's initializer list.
CFrame::CFrame(int width, int height, int * buffer){
CFrame::CFrame (int width, int height, int * buffer)
: Y (height, vector<int>(width)) {
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[i][j] = buffer [i * j + j];
}
}


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #2
Hi Jon,

Thank you for your solution.

I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame[i].reserve(width);

But that didn't work out either.

kind regards,

Koen

"Jon Bell" <jt*******@presby.edu> wrote in message
news:bq**********@jtbell.presby.edu...
In article <bq**********@gaudi2.UGent.be>,
Koen De Wolf <Ko**@raadselweb.net> wrote:

I created a 2 dimensional vector structure that contains some integer
values. The dimensions are dynamic.

When I want to add values to it, using [r][c], I always get an "Access
violation" error.

/* header file */

#include <vector>
using namespace std ;

class CFrame {
int height, width;
vector< vector<int> > Y;


The vector Y has zero size, which means that you cannot use the []
operator either to retrieve or store data. Since it appears you know in
advance how much data is going to go into the vector, you should set its
size in the constructor before putting data into it.
public:
CFrame();
CFrame(int h,int w, int * buffer);
};

[snip]

Set the size in the constructor's initializer list.
CFrame::CFrame(int width, int height, int * buffer){


CFrame::CFrame (int width, int height, int * buffer)
: Y (height, vector<int>(width)) {
this->width = width;
this->height = height;

for (int i=1; i <height;i++)
for (int j=0; j<width; j++)
{
Y[i][j] = buffer [i * j + j];
}
}


--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA

Jul 22 '05 #3
Koen De Wolf wrote:

Hi Jon,
Please don't top post. Put your reply beneath the thing
you are replying to and snip away parts you don't noeed in your
reply. Thank you.

Thank you for your solution.

I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame[i].reserve(width);

But that didn't work out either.


You wan't resize, not reserve.
--
Karl Heinz Buchegger
kb******@gascad.at
Jul 22 '05 #4
Koen De Wolf wrote:
I also tried this:

frame.reserve(height);
for (int i=1; i <height;i++)
frame[i].reserve(width);

But that didn't work out either.


reserve only makes sure the vector has enough space for the specified
amout of elements, but doesn't create those elements. Use resize()
instead, which will default-initialize the elements.

Jul 22 '05 #5

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

Similar topics

6
by: Al Newton | last post by:
I want to use STL's sort algorithm to sort a string vector. Some of the strings are fairly long (300 to 400 chars) and the vector isn't small (5,000 to 10,000 elements). Naturally, sorting time...
6
by: kushalsoftpro | last post by:
Hi I am using STL map in VC++6.0 application. type of project is MFC DLL. My code looks like:-- typedef std::map <unsigned long,LPVOID, BOOL> MyMap; In class definition file i am using this...
6
by: young_leaf | last post by:
Using STL how do I convert a variable to a binary string but with special case which for example BYTE x, i need only the first 3 bits of this byte, is that possible? to be more specific, from...
5
by: google | last post by:
Hi All, I'm just getting started learning to use <algorithm> instead of loads of little for loops, and I'm looking for a bit of advice/mentoring re: implementing the following... I have a...
1
by: recover | last post by:
#include <xxx> int main() { const wchar* pwcHello=L"hello"; char* pcHello; xxxxxx //do something using stl cout<<pcHello<<endl; } =============out===========
8
by: writeanand | last post by:
How can I count the frequency of words in a ASCII File using STL? a) I dont know what words will be found in the file b) The max number of occurrences is 10,000 per word (in case that matters) ...
5
by: Chelong | last post by:
hey,the follow is the text file content ========================================apple====pear== one Lily 7 0 0 7 7 two Lily 20 20 6.6666 20 8 one Lily 0 10 2.85 4 0 two Lily 22 22 7.33326 2 5 ...
1
by: parvtb | last post by:
I know STL vector works. But in case STL is not available, what can one do to allocate large memory size in c++ through operator "new"? For instance, I am writing a sort algorithm, and here's...
3
by: Shilpa Sethi | last post by:
What are the advantages of using STL vectors over STL deque?
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
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,...
0
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...
0
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,...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.