views:

657

answers:

3

I think it is a simple question for you....i am pretty new in c++.....

So i have a vector defined like this:

vector<vector<float> > big_vector;

I read a file and initialized this vector, then the big_wector has about 200,000 elements in it. each is a vector < float >

Then I wanted to modify the elements in big_vector, for some elements I need to resize them first,(e.g. from 0 to 300)

big_vector[i].resize(new_size);

When I ran the program, first it went well, after some modifications, a "segmentation fault" occurred at the line above.......

Could somebody help me out? Thanks in advance...

Thanks for answering me, I have revised my question.

A: 

You have a vector of vectors.

Before you can access big_vector[i] (the vector of floats at i) you must set the size of big_vector itself.

Andrew Grant
+6  A: 

First you need to resize big_vector, so that it has some vectors to resize.

int total_vectors = 100;
big_vector.resize(total_vectors);
for(int i = 0; i < total_vectors; ++i)
     big_vector[i].resize(new_size);
James Curran
+1  A: 

Why do you need to resize vectors? Are simple push_back is not enough? Or do you set some values by index?

If you set values by index I'd recommend to use std::generate_n with std::back_inserter

Mykola Golubyev