【发布时间】:2014-08-27 18:11:38
【问题描述】:
基本上,我的代码如下所示:
vector<int> myVec; // defined elsewhere, and has stuff in it.
auto it = lower_bound(myVec.front(), myVec.back(), key);
myVec.insert(it, key); // <- compiler error!
编译器错误是error: no matching function for call to 'std::vector<int>::insert(int&, int&)',这是意外的,因为它应该是“something_something_iterator”。
为什么要这样做?
我在 Windows 7 上使用 MinGW-W64 进行编译。
【问题讨论】:
-
完整的编译器错误是什么?为什么你认为编译器认为
it是int&? -
std::vector::front/back返回一个引用或 const_reference.. 你可能想使用std::vector::begin/end -
您的编译器是否配置为使用 C++11 模式(
-std=c++11,我想,在命令行上)?否则,它可能会将auto解释为存储类说明符,并添加隐含的int。 -
完整的编译器错误是:
error: no matching function for call to 'std::vector<int>::insert(int&, int&)' -
它赋予
it与myVec.front()相同的类型,即int。这样做是因为这是指定lower_bound的方式。它推导出第一个参数的类型,并将其作为返回类型。