Reputation: 23
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.
Upvotes: 0
Views: 4631
Reputation: 59862
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
Upvotes: 1
Reputation: 103535
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);
Upvotes: 5
Reputation: 58804
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.
Upvotes: 0