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

Dan*_*lli 1 c++ stl visual-studio-2008

我在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;
}
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

c:\programmi\microsoft visual studio 9.0\vc\include\algorithm(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>'
Run Code Online (Sandbox Code Playgroud)

相同的代码由gcc编译纠正并按预期工作.

这是Visual Studio的错误吗?如何让我的示例在Visual C++ 2008上运行?

Ker*_* SB 8

你忘了#include <string>.

您必须始终包含代码所需的所有标头.永远不要依赖偶尔会发生的魔法递归包含.对于您在代码中使用的所有内容,您必须知道声明的位置,并保证声明在翻译单元中可见.

  • @Daniele事实上VS真的可以通过阻塞在typedef中第一次使用`std :: string`而不是在某些内部库函数中来帮助你.所以你不是VS编译器本身的白痴;) (2认同)