468,765 Members | 1,181 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,765 developers. It's quick & easy.

multidimensional arrays (vector)

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.
Jul 22 '05 #1
3 1896
Peter L. wrote:

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




Jul 22 '05 #2
In article <3c**************************@posting.google.com >,
Peter L. <pl*******@bluewin.ch> wrote:

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)

--
Jon Bell <jt*******@presby.edu> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
Jul 22 '05 #3
> 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.
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by James | last post: by
9 posts views Thread by Charles Banas | last post: by
21 posts views Thread by utab | last post: by
5 posts views Thread by asdf | last post: by
4 posts views Thread by Gregory.A.Book | last post: by
4 posts views Thread by tvashtar | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.