std::find on std::vector< std::string > 在 Visual C++ 2008 中无法编译?

std::find on std::vector< std::string > does not compile in Visual C++ 2008?

本文关键字:std 2008 编译 C++ gt on find vector lt string Visual      更新时间:2023-10-16

我在Visual C 2008 Express Edition上尝试了此代码,但没有编译:

#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
    typedef std::string Element;
    typedef std::vector< Element > Vector;
    typedef Vector::iterator Iterator;
    Vector v;
    std::find( v.begin(), v.end(), std::string( "xxx" ) );
    return 0;
}

我有以下错误:

c:programmimicrosoft visual studio 9.0vcincludealgorithm(40) : error C2784: 'bool  std::operator ==(const std::vector<_Ty,_Alloc> &,const std::vector<_Ty,_Alloc> &)' : could not deduce template argument for 'const std::vector<_Ty,_Alloc> &' from 'std::basic_string<_Elem,_Traits,_Ax>'

GCC校正了相同的代码,并按照预期的方式进行编译。

它是Visual Studio的错误吗?我该如何使我的示例在Visual C 2008上工作?

您忘记了#include <string>

您必须始终包含代码所需的所有标题。切勿依靠有时碰巧起作用的魔术递归包含物。对于您在代码中使用的所有内容,您必须知道已声明的位置,并保证在翻译单元中可见声明。