【问题标题】:STL vector.insert method expects _InputIterator as the argumentsSTL vector.insert 方法需要 _InputIterator 作为参数
【发布时间】:2018-01-08 20:56:00
【问题描述】:

我有以下代码:

typedef unsigned long int   U64;
std::vector<U64> vectorA;
std::vector<U64> vectorB;
vectorA.insert(vectorA.end(), vectorB.begin(), vectorB.end());

最后一行出现编译错误,找不到方法。

它需要以下签名(代码来自 stl_vector.h):

   template<typename _InputIterator,
           typename = std::_RequireInputIter<_InputIterator>>
    iterator
    insert(const_iterator __position, _InputIterator __first,
           _InputIterator __last)

如何获取向量类实例的 _InputIterator / _RequireInputIter 实例? 我可以用不同的方法做同样的事情吗?

我正在使用 gcc:

gcc 版本 7.0.1 20170407(实验性)[主干修订版 246759] (Ubuntu 7-20170407-0ubuntu2)

和 Ubuntu:

NAME="Ubuntu" VERSION="17.04 (Zesty Zapus)"

编辑:

我收到编译错误:

Invalid arguments ' Candidates are:
__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>> insert(__gnu_cxx::__normal_iterator<const int
*,std::vector<int,std::allocator<int>>>, const int &)
__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>> insert(__gnu_cxx::__normal_iterator<const int
*,std::vector<int,std::allocator<int>>>, int &&)
__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>> insert(__gnu_cxx::__normal_iterator<const int
*,std::vector<int,std::allocator<int>>>, std::initializer_list<int>)
__gnu_cxx::__normal_iterator<int *,std::vector<int,std::allocator<int>>> insert

【问题讨论】:

  • minimal reproducible example,拜托。这应该像书面的那样工作。
  • U64 是什么,您遇到了什么错误?
  • vector::begin() 和 end() 是输入迭代器。
  • 除了 typedef,您的代码中与我链接的 minimal reproducible example 有什么不同?不管是什么,请把你的minimal reproducible example 放在一起,重复这个问题。
  • 非常感谢您的帮助,我的代码也出现了同样的编译错误。我将尝试提供有关我的项目/编译器设置的更多信息。

标签: c++ vector stl iterator


【解决方案1】:

这些代码可以编译并正常工作。用https://wandbox.org/测试过

#include <vector>
#include <iostream>

typedef unsigned long int   U64;
int main()
{
    std::vector<U64> vectorA = { 3, 4};
    std::vector<U64> vectorB = { 5, 7};
    vectorA.insert(vectorA.end(), vectorB.begin(), vectorB.end());

    for (const auto& i: vectorA)
      std::cout << i << ' ';
}

有些事情你没有告诉我们。

请注意,这里使用的不是 STL。 STL 是 90 年代创建的遗留库。我们现在使用的是标准的 C++ 库,它基于 STL 并在一定程度上得到了提升。由于历史原因,STL 仍然存在。但是 STL 不符合 C++ 标准。包含路径或工具链有问题。有可能您实际上尝试使用 vanilla STL 标头进行编译,或者某些标头与 boost 标头混合在一起。

【讨论】:

  • Swift 的代码也可以按预期使用 g++ (Ubuntu 5.4.0-6ubuntu1~16.04.4) 5.4.0 20160609 进行编译和运行
  • @systemcpro 我想,wandbox 在 debian 上运行,你可以用那个东西尝试各种编译器:p
  • 谢谢。也只是在那里玩。所以我不知道。我认为需要更多信息。
  • @systemcpro 我刚刚意识到我可以通过system("uname -a"); 来检查 XD
  • 这不是 STL,但大多数 C++ 程序员不知道其中的区别,认为 STL 要么是 C++ 标准库的名称,要么是它的子集。混入实际 STL 标头的可能性很小,尤其是考虑到编译器错误消息表明这是 libstdc++。
猜你喜欢
  • 1970-01-01
  • 2020-09-02
  • 1970-01-01
  • 2014-02-17
  • 1970-01-01
  • 1970-01-01
  • 2012-09-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多