【问题标题】:How to insert a vector in the defined place of 2D vector?如何在二维向量的定义位置插入向量?
【发布时间】:2017-09-27 13:41:08
【问题描述】:

我有一个 std::string 的简单二维向量。

std::vector<std::vector<std::string> > vec;
std::vector<std::string> v;

现在如何在 vec 的定义位置插入 v?

我尝试使用insert(),但出现错误:

 vec.insert(k,v); //k -is an position of vec for inserting


no matching function for call to std::vector<std::vector<std::basic_string<char> > >::insert(int, std::vector<std::basic_string<char> >&)

【问题讨论】:

  • k 是什么?请提供minimal reproducible example
  • 为 k 添加了评论
  • 不能添加定义吗? cmets 可以说任何话,与您收到的错误消息完全无关

标签: c++ vector insert 2d-vector


【解决方案1】:

使用迭代器:

std::vector<std::vector<std::string> > vec;
std::vector<std::string> v;
vec.insert(vec.begin(), v);

或:

vec.insert(vec.begin() + 2, v);

在位置 2 插入,但确保有索引 2 - 即。调整矢量大小:vec.resize(3);

从你的错误中我看到 k 的类型是 int :insert(int,,你需要一个迭代器。

【讨论】:

    猜你喜欢
    • 2013-10-29
    • 2021-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 2022-11-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多