c++ - 如何将 vector 插入多维 vector ?

标签 c++ vector

假设我有 vector 的 vector

vector< vector<int> > bigTable;

vector<int> data;
data.resize(2);
fingertable.resize(5,data);

如何插入 vector ?

    vector<int> newData;
    newData.resize(2);
    newData.push_back(123);
    newData.push_back(456);

当我执行以下操作时,bigTable vector 中的数据仍然为 0。

bigTable.push_back(newData);
    cout << bigTable[0][0]; // this will produce an output of 0

最佳答案

我认为您误解了 resize 的作用(也许您在考虑 reserve?)。在您的第二个代码片段之后,newData 将包含 4 个元素:00123456

关于c++ - 如何将 vector 插入多维 vector ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10486236/

相关文章:

c++ - 函数原型(prototype)设计 - C++

c++ - 在 Boost multi_array 中进行维度排列

c++ - 在 Windows 8.1 中运行 Visual Studio 6 C++

C++将带有构造函数的对象插入 map

c++ - 如何在类的构造函数中初始化 vector

C++:按 vector 大小重载函数

c++ - 使用简单的 OpenGL 命令 glvertex2i、glColor3ub 时出现 GL_INVALID_OPERATION 错误 1218 (0x0502)

c++ - C++ 中 vector<double> 的 ArgMin?

c++ - 优化循环 C++

c++ - 如何在 vector 中搜索结构项?