我认为这对你来说是一个简单的问题......我是c ++新手......
所以我有一个这样定义的向量:
vector; > big_vector;
我读取了一个文件并初始化了这个向量,然后 big_wector 中有大约 200,000 个元素。 每个都是一个向量 < 浮动>
然后我想修改big_vector中的元素,对于某些元素我需要先调整它们的大小,(例如从0到300)
big_vector[i].resize(new_size);
当我运行程序时,起初一切顺利,经过一些修改,上面的行出现了“分段错误”。
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.
发布评论
评论(3)
首先,您需要调整 big_vector 的大小,以便它有一些要调整大小的向量。
First you need to resize big_vector, so that it has some vectors to resize.
为什么需要调整向量大小? 简单的push_back还不够吗? 或者你通过索引设置一些值?
如果您按索引设置值,我建议将 std::generate_n 与 std::back_inserter 一起使用
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
你有一个向量的向量。
在访问 big_vector[i](i 处的浮点数向量)之前,您必须设置 big_vector 本身的大小。
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.