将向量插入到向量向量中

Inserting a vector into a vector of vectors

本文关键字:向量 插入      更新时间:2023-10-16

我正在制作一种使用向量向量的插入排序程序。我想知道如何将向量插入向量中。

// constructing vector of vectors
vector< vector< int > > v_of_v;
for (int i = 0; i < 3; i++) {
vector<int> row(2, i*100);
v_of_v.push_back(row);
}
vector<int> tmp(2, 1);
// predetermined index
index = 2;
v_of_v.insert(index, tmp); // doesnt work

我看到的示例只是遍历 tmp 并插入了向量的每个元素,这不是我要找的。我希望能够插入矢量本身,就像push_back一样。

试试这个:

v_of_v.insert(v_of_v.begin() + index, tmp);