By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,159 Members | 913 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,159 IT Pros & Developers. It's quick & easy.

multidimensional arrays (vector)

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
> 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.