multidimensional arrays (vector)

P

Peter L.

It would be great if someone could give me a hint
for my problem.

I create a multidimensional array:

vector< vector<string> > applications;

and then I want to write into it.

applications[0][0] = "Test";

Why do I get an segmentation fault error?
I also tried to resize the array before, but
it doesn't work.

Thank you very much for your response.
 
M

Max M.

Peter said:
vector< vector<string> > applications;

and then I want to write into it.

applications[0][0] = "Test";

Why do I get an segmentation fault error?
I also tried to resize the array before, but
it doesn't work.

applications.resize(1);
applications[0].resize(1);

applications[0][0] = "Test";


Max
 
J

Jon Bell

vector< vector<string> > applications;

and then I want to write into it.

applications[0][0] = "Test";

Why do I get an segmentation fault error?

Because as far as the vector is concerned, the location applications[0][0]
doesn't exist. The vector currently has zero size, so no memory has been
allocated for actual data.
I also tried to resize the array before, but
it doesn't work.

It's hard to tell you what you did wrong, because you didn't show the code
that you tried. :-(

Someone else has already showed you how to use resize() to create the
location applications[0][0]. I'll also point out that you can set your
vector to a suitable initial size when you construct it:

vector<vector<string> > applications (numRows, vector<string>(numCols));

using suitable values for numRows and numCols. (this will make all the
rows the same size, namely numCols)
 
C

Chris Mantoulidis

I create a multidimensional array:
vector< vector<string> > applications;

and then I want to write into it.

You don't say how many items it will have. Since I haven't really
experimented with multidimensional vectors, I will give you a
onedimensional vector example:

vector<int> abc(20);

which creates 20 items. Do NOT confuse (20) with [20] because [20]
will create 20 empty vectors and you will get many error messages.
applications[0][0] = "Test";

Why do I get an segmentation fault error?
I also tried to resize the array before, but
it doesn't work.

Well, a possible reason would be because it doesn't know string size
so that i can be resized. But since I am not sure, it might not be
that.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top