【问题标题】:Vector find function working in Visual Studio but not in GCC向量查找函数在 Visual Studio 中工作但在 GCC 中不工作
【发布时间】:2015-11-28 22:58:56
【问题描述】:

我有一个函数可以在 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 不仅不能识别这个错误,而且还能正确地执行任务。

【问题讨论】:

    标签: c++ visual-studio c++11 g++


    【解决方案1】:

    你没有#include &lt;algorithm&gt;the header in which std::find lives

    When I add it, your code compiles in GCC.

    Visual Studio 的标准库实现,纯粹是偶然的,必须组织成你的标题#include 碰巧自己最终出现#includeing &lt;algorithm&gt;

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

    【讨论】:

      猜你喜欢
      • 2020-10-16
      • 2014-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-07
      • 2014-12-30
      • 2019-03-21
      • 1970-01-01
      相关资源
      最近更新 更多