c++ - 插入二维 vector

标签 c++ stl vector

在为内部 vector 分配内存的同时,你能在一行中完成插入操作吗?

  vector <vector<int>> myvector;

  int a[] = {0, 1, 2, 3, 4};

  for (int index = 0; index < 2; index++)
  {
      myvector.push_back(vector<int>()); //allocate memory for internal vector
      myvector[index].insert(myvector[index].begin(), a, &a[5]); //insert
  }

最佳答案

是的,std::vector 有一个带有一对迭代器的模板构造函数,因此您可以使用:

myvector.push_back( std::vector<int>( a, a + 5 ) );

一对指针作为一对迭代器。

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

相关文章:

C++ 头文件 - 困惑!

c++ - 每个键具有多个值的 QSettings

c++ - c++中 vector 的clear()和erase()方法之间的区别?

c++ - 切片 vector

c++ - Vector在C++中的实现

C++ UNIX 错误 : Undefined first referenced symbol in file

c++ - 我可以使用模板而不是宏来创建异常类吗?

c++ - 为什么添加静态数据成员会导致链接器失败?

c++ - 如果有两个 "greatest"索引,我如何找到 vector 中最大值的索引,默认为更大的索引?

c++ - 在 C++ 中将对象 vector 保存到文件