c++ - 比较 C++ 中的两个 STL 列表

标签 c++ c++11 stl

我有以下代码:

IndexedData index(strUrl);// IndexedData is a class.
jsonParser->parse(strTags,index,ret); 

std::list<odIndexedDataNode> tags; // so index.tags is a STL list.

std::list<odIndexedDataNode> testListIdns;
insertTags(testListIdns);

这个testListIdns也是一个STL列表。我想比较 index.tags 和 testListIdns 是否相等。

我写了这个函数:

bool isEqual(std::list<odIndexedDataNode> &tags, std::list<odIndexedDataNode> &testListIdns) {
    if(tags.size() == testListIdns.size())
        return std::equal(tags.begin(), tags.end(), testListIdns.begin());
    else
        return false;
}

但它给出了以下编译错误。谁能帮帮我?

In file included from /usr/include/c++/4.6/bits/char_traits.h:41:0,
                 from /usr/include/c++/4.6/ios:41,
                 from /usr/include/c++/4.6/istream:40,
                 from /usr/include/c++/4.6/sstream:39,
                 from /root/target/include/boost/test/utils/wrap_stringstream.hpp:26,
                 from /root/target/include/boost/test/predicate_result.hpp:20,
                 from /root/target/include/boost/test/test_tools.hpp:19,
                 from /root/target/include/boost/test/unit_test.hpp:19,
                 from configuration_test.cpp:11:
/usr/include/c++/4.6/bits/stl_algobase.h: In static member function ‘static bool std::__equal<_BoolType>::equal(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>, bool _BoolType = false]’:
/usr/include/c++/4.6/bits/stl_algobase.h:820:71:   instantiated from ‘bool std::__equal_aux(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>]’
/usr/include/c++/4.6/bits/stl_algobase.h:1022:37:   instantiated from ‘bool std::equal(_II1, _II1, _II2) [with _II1 = std::_List_iterator<Rubicon::odIndexedDataNode>, _II2 = std::_List_iterator<Rubicon::odIndexedDataNode>]’
configuration_test.cpp:58:67:   instantiated from here
/usr/include/c++/4.6/bits/stl_algobase.h:791:6: error: no match for ‘operator==’ in ‘__first1.std::_List_iterator<_Tp>::operator* [with _Tp = Rubicon::odIndexedDataNode, std::_List_iterator<_Tp>::reference = Rubicon::odIndexedDataNode&]() == __first2.std::_List_iterator<_Tp>::operator* [with _Tp = Rubicon::odIndexedDataNode, std::_List_iterator<_Tp>::reference = Rubicon::odIndexedDataNode&]()’
/usr/include/c++/4.6/bits/stl_algobase.h:791:6: note: candidates are:
/usr/include/c++/4.6/bits/postypes.h:218:5: note: template<class _StateT> bool std::operator==(const std::fpos<_StateT>&, const std::fpos<_StateT>&)
/usr/include/c++/4.6/bits/stl_pair.h:201:5: note: template<class _T1, class _T2> bool std::operator==(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/include/c++/4.6/bits/stl_iterator.h:285:5: note: template<class _Iterator> bool std::operator==(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/include/c++/4.6/bits/stl_iterator.h:335:5: note: template<class _IteratorL, class _IteratorR> bool std::operator==(const std::reverse_iterator<_IteratorL>&, const std::reverse_iterator<_IteratorR>&)
/usr/include/c++/4.6/bits/allocator.h:122:5: note: template<class _T1, class _T2> bool std::operator==(const std::allocator<_T1>&, const std::allocator<_T2>&)
/usr/include/c++/4.6/bits/allocator.h:127:5: note: template<class _Tp> bool std::operator==(const std::allocator<_Tp1>&, const std::allocator<_Tp1>&)
/usr/include/c++/4.6/bits/basic_string.h:2427:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2434:5: note: template<class _CharT> typename __gnu_cxx::__enable_if<std::__is_char<_Tp>::__value, bool>::__type std::operator==(const std::basic_string<_CharT>&, const std::basic_string<_CharT>&)
/usr/include/c++/4.6/bits/basic_string.h:2448:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const _CharT*, const std::basic_string<_CharT, _Traits, _Alloc>&)
/usr/include/c++/4.6/bits/basic_string.h:2460:5: note: template<class _CharT, class _Traits, class _Alloc> bool std::operator==(const std::basic_string<_CharT, _Traits, _Alloc>&, const _CharT*)
/usr/include/c++/4.6/bits/streambuf_iterator.h:194:5: note: template<class _CharT, class _Traits> bool std::operator==(const std::istreambuf_iterator<_CharT, _Traits>&, const std::istreambuf_iterator<_CharT, _Traits>&)
/usr/include/c++/4.6/bits/stream_iterator.h:130:5: note: template<class _Tp, class _CharT, class _Traits, class _Dist> bool std::operator==(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&, const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&)
/usr/include/c++/4.6/bits/stl_list.h:275:5: note: template<class _Val> bool std::operator==(const std::_List_iterator<_Tp>&, const std::_List_const_iterator<_Val>&)
/usr/include/c++/4.6/bits/stl_list.h:1564:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::list<_Tp, _Alloc>&, const std::list<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_vector.h:1273:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::vector<_Tp, _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_tree.h:309:5: note: template<class _Val> bool std::operator==(const std::_Rb_tree_iterator<_Tp>&, const std::_Rb_tree_const_iterator<_Val>&)
/usr/include/c++/4.6/bits/stl_tree.h:856:5: note: template<class _Key, class _Val, class _KeyOfValue, class _Compare, class _Alloc> bool std::operator==(const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&, const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_map.h:882:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::map<_Key, _Tp, _Compare, _Alloc>&, const std::map<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_multimap.h:800:5: note: template<class _Key, class _Tp, class _Compare, class _Alloc> bool std::operator==(const std::multimap<_Key, _Tp, _Compare, _Alloc>&, const std::multimap<_Key, _Tp, _Compare, _Alloc>&)
/usr/include/c++/4.6/bits/stl_deque.h:246:5: note: template<class _Tp, class _Ref, class _Ptr> bool std::operator==(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const std::_Deque_iterator<_Tp, _Ref, _Ptr>&)
/usr/include/c++/4.6/bits/stl_deque.h:253:5: note: template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool std::operator==(const std::_Deque_iterator<_Tp, _RefL, _PtrL>&, const std::_Deque_iterator<_Tp, _RefR, _PtrR>&)
/usr/include/c++/4.6/bits/stl_deque.h:1917:5: note: template<class _Tp, class _Alloc> bool std::operator==(const std::deque<_Tp, _Alloc>&, const std::deque<_Tp, _Alloc>&)
/usr/include/c++/4.6/bits/stl_stack.h:241:5: note: template<class _Tp, class _Seq> bool std::operator==(const std::stack<_Tp, _Seq>&, const std::stack<_Tp, _Seq>&)

最佳答案

看起来您的类型 odIndexedDataNode 没有运算符 ==。您应该重载它,或者编写函数/仿函数并将其作为第四个参数发送给 std::equal

关于c++ - 比较 C++ 中的两个 STL 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29604564/

相关文章:

c++ - 在 boolean 数组中输入

c++ - 如何将参数传递给 std::vector::insert()?

c++ - 如何设置 VS Code 在 OSX 中构建、运行和调试简单的 C++ 文件?

c++11 - 在具有复制和移动向量和复制赋值的 C++ 类中 : no need for move assignment?

c++ - 如何声明指向 C++11 std::array 的指针?

c++ - Kill -9时堆内存会被释放吗?

C++11 gcc : explicit qualification in declaration? 标准引用?

c++ - 将 "this"指针转换为字符串

c++ - std::remove 无法正常工作,仍然有额外的元素

c++ - 当有人尝试在当前 .cpp 或 .h 文件中使用 STL 时显示错误(在 Visual Studio 中)