如何在 Vector<Vector<float>*>* 中写入数据?

how to write data in vector<vector<float>*>*?

本文关键字:gt lt Vector 数据 float      更新时间:2023-10-16

我正在尝试为向量的向量分配空间,但在分配后 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);

在这种情况下,大小index2vector直接内置到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)
}
  1. 仅尾随下划线不同的变量是自找麻烦。
  2. unsigned除了签名/未签名错误的风险外,不会添加任何
  3. 内容
  4. 索引应该是i而不是index1
  5. 最好使用局部变量而不是堆分配和指针,但这会过多地更改代码。

更改

(*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) );
}

现场演示