向量的push_back向量
push_back vector of vector
我得到了一些向量的向量问题。在我的程序中,我为vector的vector定义了一个动态内存,并对元素进行了resize和pushback操作。
vector<vector<double> > *planes = new vector<vector<double> >
planes->resize(s_list->size()); // size of another vector that i need to use
vector<int>::iterator s_no;
for(s_no=s_list->begin(), int i=0; s_no!=s_list->end(); s_no++, i++){){
//where i i the indices of planes
//some codes for computing length, width
planes->at(i).push_back(lenght);
planes->at(i).push_back(width);
}
它起作用了,我得到了我添加的所有值的打印。然后,我将新的矢量定义部分更改为
vector<vector<double> > *planes =
new vector<vector<double> >(s_list->size(),vector<double>(2,0.0))
并移除了调整大小的部分。然后,当我得到向量的输出时,我得到了所有的0值。你能纠正这个问题吗。
使用at
而不是push_back
planes->at(i).at(0)=lenght;
planes->at(i).at(1)=width;
push_back()添加了新项目,因此在每个向量中以4个项目结束。您应该使用at()来修改现有的条目。
更好的方法是使用vector< pair<double,double> >
,假设您总是有两个项目。
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 我们可以使用Back() - 值索引在其上执行向量插入
- vector.back() 可以用来为向量的最后一个元素赋值吗?
- 修改向量后引用 vector.back() 的奇怪行为
- 在 C++ 中将元素添加到空向量:为什么 push.back 有效而 [] 无效
- Vector_push back未填充向量(c++)
- C++为空向量上的向量返回什么::back()
- 我如何修复这段代码,使它不会pop_back或back()如果向量/堆栈是空的
- 向量resize(), push.back(), reserve()方法