我想向一些同事总结std::find
的工作原理,我想向他们展示在std::map
上使用它的技巧(以及为什么他们不应该这样做)是多么棘手,所以我开始摆弄编译器资源管理器。
我想我遇到了libc++
和libstdc++
之间的实现差异,因为以下代码段是在前者上编译的
#include <string>
#include <map>
int main (){
std::map<std::string, int> myMap;
myMap["string1"] = 100;
std::map<std::string, int>::value_type element("string1", 100);
auto it = std::find(myMap.begin(), myMap.end(), element);
}
但是无法与后者进行编译,从而产生以下错误
error: no matching function for call to 'find'
auto it = std::find(myMap.begin(), myMap.end(), element);
^~~~~~~~~
/opt/compiler-explorer/gcc-9.2.0/lib/gcc/x86_64-linux-gnu/9.2.0/../../../../include/c++/9.2.0/bits/streambuf_iterator.h:373:5: note: candidate template ignored: could not match 'istreambuf_iterator' against '_Rb_tree_iterator'
find(istreambuf_iterator<_CharT> __first,
^
1 error generated.
所以我很困惑,我想知道两者中哪一个是理想的行为。编译器资源管理器链接:
带libc++的
最佳答案
您必须按照文档here的#include <algorithm>
进行操作。
您只是(不幸)幸运地发现一个库为您隐式包含了该 header ,但您实际上不应该依赖它。
关于c++ - 在libc++和libstdc++之间的std::map上使用std::find时的实现差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556025/