STL vector.insert 方法期望_InputIterator作为参数



我有以下代码:

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

这些代码编译和工作没有错误。经 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年代的遗留库。我们现在使用的是基于STL的标准C++库,并在一定程度上进行了提升。由于历史原因,STL仍然存在。但STL不符合C++标准。 包含路径或工具链有问题。您可能实际上尝试使用原版 STL 标头进行编译,或者某些标头与 boost 标头混淆。

最新更新