向量等于类型在 g++ 工作但不在 Visual Studio 2010 中

发布于 2025-01-01 04:44:28 字数 4112 浏览 1 评论 0原文

#include<iostream>
#include<vector>
std::vector<std::string> vector1;
int main() {
    vector1.push_back("adadad");
    std::vector<std::string> vector2;
    vector2.push_back("adadd");
    if (vector1==vector2) {
        std::cout<<"success";
    } else {
       vector1.swap(vector2);
       vector2.clear();
       vector2.push_back("adadd");
       if (vector1==vector2) {
           std::cout<<"success_swap";
       }
    }
}

现在这可以在 g++ 中运行,但不能在 Visual Studio 中运行。运算符 == 在这里不起作用,并在 Visual Studio 2010(ultimate) 中引发编译错误。 如果向量是整数类型,则同样有效。我在这里遗漏了什么吗?这不是他们忽略的 Linux 原生的东西。为什么gcc中有实现,而vc++中没有?

它显示的错误消息是:

[snip]\vc\include\xutility(2990): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
          [snip]\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
          [snip]\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
          while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::basic_string<_Elem,_Traits,_Ax>)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\xutility(3030) : see reference to function template instantiation 'bool std::_Equal<_InIt1,_InIt2>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\vc\include\xutility(3051) : see reference to function template instantiation 'bool std::_Equal1<const std::basic_string<_Elem,_Traits,_Ax>*,_InIt2>(_InIt1,_InIt1,_InIt2,std::tr1::true_type)' being compiled
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
          ]
          [snip]\vc\include\vector(1489) : see reference to function template instantiation 'bool std::equal<std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
              _InIt1=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\test\main.cpp(8) : see reference to function template instantiation 'bool std::operator ==<std::string,std::allocator<_Ty>>(const std::vector<_Ty> &,const std::vector<_Ty> &)' being compiled
          with
          [
              _Ty=std::string
          ]
#include<iostream>
#include<vector>
std::vector<std::string> vector1;
int main() {
    vector1.push_back("adadad");
    std::vector<std::string> vector2;
    vector2.push_back("adadd");
    if (vector1==vector2) {
        std::cout<<"success";
    } else {
       vector1.swap(vector2);
       vector2.clear();
       vector2.push_back("adadd");
       if (vector1==vector2) {
           std::cout<<"success_swap";
       }
    }
}

Now this works in g++ but not in visual studio. The operator == doesn't work here and throws compilation error in visual studio 2010(ultimate). The same works if the vector is of a integer type.Am I missing something here ? This is not a linux native thing that they have omitted. Why there is a implementation in gcc but not in vc++ ?

The error message that it shows is:

[snip]\vc\include\xutility(2990): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
          [snip]\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
          [snip]\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
          [snip]\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
          while trying to match the argument list '(const std::basic_string<_Elem,_Traits,_Ax>, const std::basic_string<_Elem,_Traits,_Ax>)'
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>
          ]
          [snip]\vc\include\xutility(3030) : see reference to function template instantiation 'bool std::_Equal<_InIt1,_InIt2>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\vc\include\xutility(3051) : see reference to function template instantiation 'bool std::_Equal1<const std::basic_string<_Elem,_Traits,_Ax>*,_InIt2>(_InIt1,_InIt1,_InIt2,std::tr1::true_type)' being compiled
          with
          [
              _Elem=char,
              _Traits=std::char_traits<char>,
              _Ax=std::allocator<char>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt1=const std::basic_string<char,std::char_traits<char>,std::allocator<char>> *
          ]
          [snip]\vc\include\vector(1489) : see reference to function template instantiation 'bool std::equal<std::_Vector_const_iterator<_Myvec>,std::_Vector_const_iterator<_Myvec>>(_InIt1,_InIt1,_InIt2)' being compiled
          with
          [
              _Myvec=std::_Vector_val<std::string,std::allocator<std::string>>,
              _InIt1=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>,
              _InIt2=std::_Vector_const_iterator<std::_Vector_val<std::string,std::allocator<std::string>>>
          ]
          [snip]\test\main.cpp(8) : see reference to function template instantiation 'bool std::operator ==<std::string,std::allocator<_Ty>>(const std::vector<_Ty> &,const std::vector<_Ty> &)' being compiled
          with
          [
              _Ty=std::string
          ]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

栀梦 2025-01-08 04:44:28

发生错误的原因是在 MSVC 中,std::string 的相等运算符 (==) 不包含在 中。 ;<向量>。您还必须包含

消息中的关键行是; “错误 C2678:二进制 '==':找不到采用 'const std::basic_string<_Elem,_Traits,_Ax>' 类型的左侧操作数的运算符(或者没有可接受的转换)”

The error happens because in MSVC, std::string's equality operator (==) is not included by <iostream> or <vector>. You have to include <string> as well.

The key line in the message is; "error C2678: binary '==' : no operator found which takes a left-hand operand of type 'const std::basic_string<_Elem,_Traits,_Ax>' (or there is no acceptable conversion)"

明媚如初 2025-01-08 04:44:28

您可能使用的是非常旧的编译器。

这在 MSVS 2005 中对我有用,MSVS 2005 本身并不是那么新。

如果您的编译器兼容 C++03,它应该可以工作:

23.2.4

模板
bool 运算符 == ( const vector& x,
                   const 向量& y);

You're probably using a really old compiler.

This works for me in MSVS 2005, which isn't that new itself.

If your compiler is C++03 compliant, it should work:

23.2.4

template <class T, class Allocator>
bool operator == ( const vector<T,Allocator>& x,
                   const vector<T,Allocator>& y);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文