如何在 Vector<Vector<float>*>* 中写入数据?
how to write data in vector<vector<float>*>*?
我正在尝试为向量的向量分配空间,但在分配后 gdb 在 pVectorTemp_
中显示为空,但显示大小为 2
#include <iostream>
#include<vector>
using namespace std;
int main( )
{
int index1 = 2;
int index2 = 2;
vector<vector<float>*>* pVectorTemp_ = NULL;
pVectorTemp_ = new vector<vector<float>*>();
pVectorTemp_->resize(index1);
for(unsigned int i=0 ;i< index1;i++)
{
vector<float>* pvecTemp = new vector<float>();
pvecTemp->resize(index2);
(*pVectorTemp_)[index1] = (pvecTemp);
}
return 0;
}
GDB :
(gdb) pvector pVectorTemp_
elem[0]: $2 = (std::vector<float, std::allocator<float> > *) 0x0
elem[1]: $3 = (std::vector<float, std::allocator<float> > *) 0x0
Vector size = 2
Vector capacity = 2
那么我做错了什么吗?
在for
循环体中,您有:
(*pVectorTemp_)[index1] = (pvecTemp);
但请注意,for
循环的索引是i
(index1
是上限(。
所以,我认为你有一个错别字或错误,你可能想使用i
(不是index1
(作为[...]
内的索引。
另请注意,您有一个 signed
/unsigned
不匹配,因为在循环中您将unsigned int
作为索引,并将其与 index1
进行比较,这是一个有符号整数。
但是,无论如何,您的代码是无用的复杂。
您不需要使用 new
在堆上分配所有这些向量。
只需使用自动("堆栈"(分配,例如:
int main()
{
int index1 = 2;
int index2 = 2;
// Your original code is commented out:
//
// vector<vector<float>*>* pVectorTemp_ = NULL;
// pVectorTemp_ = new vector<vector<float>*>();
vector<vector<float>> vectorTemp;
// pVectorTemp_->resize(index1);
// Just consider calling push_back.
for (int i = 0; i < index1; i++)
{
// vector<float>* pvecTemp = new vector<float>();
// pvecTemp->resize(index2);
// (*pVectorTemp_)[index1] = (pvecTemp);
vectorTemp.push_back(vector<float>(index2));
}
// No need for return 0 in main().
// return 0;
}
看看代码是如何简化的!
(下面是不带注释的代码。
int main()
{
int index1 = 2;
int index2 = 2;
vector<vector<float>> vectorTemp;
for (int i = 0; i < index1; i++)
{
vectorTemp.push_back(vector<float>(index2));
}
}
作为进一步的改进,假设您的 C++ STL 实现提供了这一点,您可能希望使用 emplace_back()
而不是 push_back()
来构建嵌套向量:
// Instead of vectorTemp.push_back(vector<float>(index2));
//
vectorTemp.emplace_back(index2);
在这种情况下,大小index2
的vector
直接内置到vectorTemp
("外部"向量(容器中,无需临时。
您可能还想在StackOverflow上阅读此线程以获取更多详细信息:
push_back与emplace_back
这些是我能找到的东西。
vector<vector<float>*>* pVectorTemp1 = new vector<vector<float>*>(); // (1)(4)
pVectorTemp1->resize(index1);
for (int i=0 ; i < index1; i++) { // (2)
vector<float>* pvecTemp2 = new vector<float>(); /(4)
pvecTemp2->resize(index2);
(*pVectorTemp1)[i] = (pvecTemp2); // (3)
}
- 仅尾随下划线不同的变量是自找麻烦。
-
unsigned
除了签名/未签名错误的风险外,不会添加任何
内容 - 索引应该是
i
而不是index1
。 - 最好使用局部变量而不是堆分配和指针,但这会过多地更改代码。
更改
(*pVectorTemp_)[index1] = (pvecTemp);
跟
(*pVectorTemp_)[i] = (pvecTemp);
那么我做错了什么吗?
是的,你做错了什么:
- 在不需要时使用动态分配
- 使用不必要的原始循环
这是一个简化版本:
int main( )
{
int index1 = 2;
int index2 = 2;
std::vector<std::vector<float>> pVectorTemp_
( index1
, std::vector<float>(index2) );
}
现场演示
相关文章:
- 请解释这句话(cout<<1+int((a<b)^((b-a)&1) )<<endl
- 呼叫运营商<<临时
- 如何防止clang格式在流运算符调用之间添加换行符<<
- <<操作员在下面的行中工作
- EASTL矢量<向量<int>>连续的
- C - 创建矢量&lt; vector&lt; double&gt;&gt;矩阵具有分配而不是inizializ
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- C :对矢量进行排序&lt; struct&gt;(结构有2个整数)基于结构的整数之一
- 明确的专业化“ CheckIntmap&lt;&gt;”实例化
- 什么是模板&lt;&gt;inline bla bla
- 编辑C Qlist&lt; object*&gt; gt;QML代码和一些QML警告中的模型
- eigen :: llt&lt;eigen :: matrixxd&gt;具有不完整的类型
- 错误,包括&lt; ctype&gt;在原子上使用C 11
- std::vector<;uint8_t>;当C++11/14启用时,手动复制而不是调用memcpy
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 是std :: set&lt; std :: future&gt;不可能存在
- 是numeric_limits&lt; int&gt; :: is_modulo从逻辑上矛盾
- opencv 2.4.7在iOS错误背景_segm.hpp #include&lt; list&gt;未找到
- 在修改列表后,std :: list&lt; t&gt; :: end()的值是否会更改
- ///<评论></评论>在Visual Studio中