Shubhada Antapurkar
Shubhada Antapurkar

Reputation: 35

Compare two stl lists in c++

I have the following code:

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);

This testListIdns is also a STL list. I want to compare index.tags and testListIdns are equal.

I wrote this function:

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;
}

But it gives the following compilation error. Can anybody help me?

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>&)

Upvotes: 1

Views: 4851

Answers (2)

zhou
zhou

Reputation: 19

std::equal method has 2 prototypes,

template <class InputIterator1, class InputIterator2, class BinaryPredicate>  
bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2, BinaryPredicate pred);

and

template <class InputIterator1, class InputIterator2>  
bool equal (InputIterator1 first1, InputIterator1 last1,
              InputIterator2 first2);

the code you writing is the 2nd prototype,but you haven't tell the compiler that how to compare each odIndexedDataNode object,you can use the first prototype and pass the method a compare function at 4th argument

by the way,if you just want to compare each object between two collections,maybe you can use the std::set collection.

Upvotes: 1

ForEveR
ForEveR

Reputation: 55887

Looks like your type odIndexedDataNode has no operator ==. You should overload it, or write function/functor and send it as fourth argument to std::equal.

Upvotes: 1

Related Questions