矢量查找功能在 Visual Studio 中有效,但在 GCC 中无效

Vector find function working in Visual Studio but not in GCC

我在 Visual Studio 中有一个功能可以完成我想要它做的事情,我正在将它转移到 GCC 以确保在那里一切正常。

由于使用 std::find 函数,我现在遇到了很多编译错误。

我希望有人能帮我弄清楚为什么我只在 GCC 中遇到这些错误。这是代码示例:http://cpp.sh/6pky

// Example program
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
using namespace std;

int main()
{
   vector < list < pair <string, string> > > v;
   v.resize(15);
   pair<string, string> k ("foo", "bar");
   auto & whichList = v[2];
   if(find(begin(whichList), end(whichList), k) != end(whichList))
       cout << "true";

}

有问题的部分是find(begin(whichList), end(whichList), k)

我收到一条错误消息,提示我无法将成对列表与成对进行比较(我这周一直在处理这个问题),我明白这一点。我很好奇为什么VS2015不仅不识别这个错误,而且还正确地执行任务。

你没有#include <algorithm>, the header in which std::find lives.

When I add it, your code compiles in GCC.

Visual Studio 的标准库实现,纯粹是偶然的,必须组织起来,这样你 所做的 #include 的头文件本身就会发生 [= =12=]ing <algorithm>.

Always include the correct headers for the types and functions that you use.