gpt4 book ai didi

C++。如何比较 2 个对象 vector

转载 作者:行者123 更新时间:2023-11-30 00:47:30 25 4
gpt4 key购买 nike

您好,我有 2 个对象 vector ,我想比较它们是否相等(表示 v[i] == m[i])。我的类(class)有重载运算符==,但我仍然有编译错误。

struct Node {
Node() {};
Node(int st, int end) : l(st), r(end) {
if (st < 0 || end < st) {
throw "Bad input";
}
}
Node(int st, int end, int m, int s) : l(st), r(end), sum(s), min(m) {
if (st < 0 || end < st) {
throw "Bad input";
}
}

bool operator== (Node& node) {
if (node.l == l && node.r == r && node.sum == sum && node.min == min)
return true;

return false;
}

int l = 0, r = 0;
int sum = 0;
int min = 0;
};

int main() {
vector<Node> segTree {} , exampleTree {};
exampleTree.push_back(Node(0,0,1,1));

if ( segTree == exampleTree )
throw "smith";

return 0;
}

错误:

In file included from test.cpp:1:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iostream:38:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/ios:216:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/__locale:15:
In file included from /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/string:439:
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:658:97: error: invalid operands to binary expression
('const Node' and 'const Node')
_LIBCPP_INLINE_VISIBILITY bool operator()(const _T1& __x, const _T1& __y) const {return __x == __y;}
~~~ ^ ~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1164:14: note: in instantiation of member function
'std::__1::__equal_to<Node, Node>::operator()' requested here
if (!__pred(*__first1, *__first2))
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:1176:19: note: in instantiation of function template
specialization 'std::__1::equal<std::__1::__wrap_iter<const Node *>, std::__1::__wrap_iter<const Node *>, std::__1::__equal_to<Node, Node> >' requested here
return _VSTD::equal(__first1, __last1, __first2, __equal_to<__v1, __v2>());
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/vector:3192:41: note: in instantiation of function template
specialization 'std::__1::equal<std::__1::__wrap_iter<const Node *>, std::__1::__wrap_iter<const Node *> >' requested here
return __sz == __y.size() && _VSTD::equal(__x.begin(), __x.end(), __y.begin());
^
test.cpp:83:18: note: in instantiation of function template specialization 'std::__1::operator==<Node, std::__1::allocator<Node> >' requested here
if ( segTree == exampleTree )
^
test.cpp:66:6: note: candidate function not viable: 'this' argument has type 'const Node', but method is not marked const
bool operator== (Node& node) {
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/utility:403:1: note: candidate template ignored: could not match
'pair<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const pair<_T1,_T2>& __x, const pair<_T1,_T2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:574:1: note: candidate template ignored: could not match
'reverse_iterator<type-parameter-0-0>' against 'const Node'
operator==(const reverse_iterator<_Iter1>& __x, const reverse_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:866:6: note: candidate template ignored: could not match
'istreambuf_iterator<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:969:1: note: candidate template ignored: could not match
'move_iterator<type-parameter-0-0>' against 'const Node'
operator==(const move_iterator<_Iter1>& __x, const move_iterator<_Iter2>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/iterator:1285:1: note: candidate template ignored: could not match
'__wrap_iter<type-parameter-0-0>' against 'const Node'
operator==(const __wrap_iter<_Iter1>& __x, const __wrap_iter<_Iter2>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/tuple:877:1: note: candidate template ignored: could not match
'tuple<type-parameter-0-0...>' against 'const Node'
operator==(const tuple<_Tp...>& __x, const tuple<_Up...>& __y)
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:1791:6: note: candidate template ignored: could not match
'allocator<type-parameter-0-0>' against 'const Node'
bool operator==(const allocator<_Tp>&, const allocator<_Up>&) _NOEXCEPT {return true;}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2876:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, const unique_ptr<_T2, _D2>& __y) {return __x.get() == __y.get();}
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2912:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(const unique_ptr<_T1, _D1>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:2920:1: note: candidate template ignored: could not match
'unique_ptr<type-parameter-0-0, type-parameter-0-1>' against 'const Node'
operator==(nullptr_t, const unique_ptr<_T1, _D1>& __x) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4716:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, const shared_ptr<_Up>& __y) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4765:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(const shared_ptr<_Tp>& __x, nullptr_t) _NOEXCEPT
^
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/memory:4773:1: note: candidate template ignored: could not match
'shared_ptr<type-parameter-0-0>' against 'const Node'
operator==(nullptr_t, const shared_ptr<_Tp>& __x) _NOEXCEPT
^
1 error generated.

最佳答案

您的operator== 应该采用const Node&,并且本身就是const。您没有修改任何一个节点!

关于C++。如何比较 2 个对象 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34723529/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com