c++ - 在libc++和libstdc++之间的std::map上使用std::find时的实现差异

标签 c++ c++11 libstdc++ libc++

我想向一些同事总结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++的
  • https://godbolt.org/z/KDsMsC
  • 带有libstdc++的
  • https://godbolt.org/z/g3DqlJ
  • 最佳答案

    您必须按照文档here#include <algorithm>进行操作。

    您只是(不幸)幸运地发现一个库为您隐式包含了该 header ,但您实际上不应该依赖它。

    关于c++ - 在libc++和libstdc++之间的std::map上使用std::find时的实现差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58556025/

    相关文章:

    c++ - native C++ 和 Windows 平板电脑

    c++11 - Boost asio thread_pool join 不等待任务完成

    c++ - 为什么 std::vector::data 和 std::string::data 不同?

    c++ - 如何编辑和重新构建 GCC libstdc++ C++ 标准库源代码?

    c++ - 最新版gcc使用libstdc++.so.5

    c++ - 是否可以在折叠表达式中插入额外的操作?

    c++ - 如何用一维数组列表初始化二维数组?

    c++ - 如何自动将处理程序添加到全局 map ?

    c++ - OpenBSD 和 FreeBSD 上未剥离 libstdc++ 系统库

    c++ - 类上的行外定义错误,但它在头文件中声明