CS253

CS253: Software Development with C++

Spring 2018

Iterators

See this page as a slide show

Iterators

CS253 Iterators

Array Traversal

You have an array, and you want to traverse (walk through) it.

int a[] = {3141, 5926, 5358, 9793, 2384, 6264};
for (int i=0; i!=6; ++i)
    cout << a[i] << ' ';
3141 5926 5358 9793 2384 6264 

Array Traversal via Pointers

Let’s do it with a pointer:

int a[] = {3141, 5926, 5358, 9793, 2384, 6264};
for (int *p = &a[0]; p != &a[6]; ++p)
    cout << *p << ' ';
3141 5926 5358 9793 2384 6264 

vector traversal

You can traverse a vector in the same way:

vector<int> a = {3141, 5926, 5358, 9793, 2384, 6264};
for (int *p = &a[0]; p != &a[6]; ++p)
    cout << *p << ' ';
3141 5926 5358 9793 2384 6264 

or, avoiding the magic number 6:

vector<int> a = {3141, 5926, 5358, 9793, 2384, 6264};
for (int *p = &a[0]; p != &a[a.size()]; ++p)
    cout << *p << ' ';
3141 5926 5358 9793 2384 6264 

Methods

vector<int> a = {3141, 5926, 5358, 9793, 2384, 6264};
for (vector<int>::iterator it = a.begin(); it != a.end(); ++it)
    cout << *it << ' ';
3141 5926 5358 9793 2384 6264 

auto is your friend

This is prettier:

vector<int> a = {3141, 5926, 5358, 9793, 2384, 6264};
for (auto it = a.begin(); it != a.end(); ++it)
    cout << *it << ' ';
3141 5926 5358 9793 2384 6264 

for loop

This is exactly the same:

vector<int> a = {3141, 5926, 5358, 9793, 2384, 6264};
for (auto v : a)
    cout << v << ' ';
3141 5926 5358 9793 2384 6264 

The for loop is defined to use .begin() and .end(), just as the previous code.

Other containers

The same iterator code works for all STL containers.

forward_list<char> l = {'a', 'c', 'k', 'J'};
for (auto it = l.begin(); it != l.end(); ++it)
    cout << *it << ' ';
a c k J 
unordered_set<char> u = {'a', 'c', 'k', 'J'};
for (auto it = u.begin(); it != u.end(); ++it)
    cout << *it << ' ';
J c k a 
set<char> s = {'a', 'c', 'k', 'J'};
for (auto it = s.begin(); it != s.end(); ++it)
    cout << *it << ' ';
J a c k 

iterator type

Iterator classifications

begin() & end()

string s = "bonehead";
cout << "First character: " << *s.begin() << '\n';
cout << "This is wrong: " << *s.end() << '\n';
First character: b
This is wrong: 
string s = "genius";
cout << "First character: " << *s.begin() << '\n';
cout << "Last character:  " << *(s.end()-1) << '\n';
First character: g
Last character:  s

.front() & .back()

Some containers have .front() and .back(), which return references to the first and last elements.

list<double> c = {1.2, 3.4, 5.6};
cout << "First: " << c.front() << '\n';
cout << "Last:  " << c.back() << '\n';
First: 1.2
Last:  5.6

These are not iterators.

Comparisons, part one

This won’t work:

list<string> l = {"kappa", "alpha", "gamma"};
for (auto it = l.begin(); it < l.end(); ++it)
    cout << *it << ' ';
c.cc: In function 'int main()':
c.cc:2:30: error: no match for 'operator<' in 'it < l.std::__cxx11::list<_Tp, 
   _Alloc>::end<std::__cxx11::basic_string<char>, 
   std::allocator<std::__cxx11::basic_string<char> > >()' (operand types are 
   'std::_List_iterator<std::__cxx11::basic_string<char> >' and 
   'std::__cxx11::list<std::__cxx11::basic_string<char> >::iterator {aka 
   std::_List_iterator<std::__cxx11::basic_string<char> >}')
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:962:5: note: candidate: 
   template<class _BiIter> bool std::__cxx11::operator<(const 
   std::__cxx11::sub_match<_BiIter>&, const std::__cxx11::sub_match<_BiIter>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:962:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1041:5: note: candidate: 
   template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool 
   std::__cxx11::operator<(std::__cxx11::__sub_match_string<_Bi_iter, 
   _Ch_traits, _Ch_alloc>&, const std::__cxx11::sub_match<_BiIter>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1041:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, 
   _Ch_alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1121:5: note: candidate: 
   template<class _Bi_iter, class _Ch_traits, class _Ch_alloc> bool 
   std::__cxx11::operator<(const std::__cxx11::sub_match<_BiIter>&, 
   std::__cxx11::__sub_match_string<_Bi_iter, _Ch_traits, _Ch_alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1121:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1198:5: note: candidate: 
   template<class _Bi_iter> bool std::__cxx11::operator<(const typename 
   std::iterator_traits<_Iter>::value_type*, const 
   std::__cxx11::sub_match<_BiIter>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1198:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::__cxx11::list<std::__cxx11::basic_string<char> 
   >::iterator {aka std::_List_iterator<std::__cxx11::basic_string<char> >}' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1272:5: note: candidate: 
   template<class _Bi_iter> bool std::__cxx11::operator<(const 
   std::__cxx11::sub_match<_BiIter>&, const typename 
   std::iterator_traits<_Iter>::value_type*)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1272:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1349:5: note: candidate: 
   template<class _Bi_iter> bool std::__cxx11::operator<(const typename 
   std::iterator_traits<_Iter>::value_type&, const 
   std::__cxx11::sub_match<_BiIter>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1349:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::__cxx11::list<std::__cxx11::basic_string<char> 
   >::iterator {aka std::_List_iterator<std::__cxx11::basic_string<char> >}' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:62:0,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1429:5: note: candidate: 
   template<class _Bi_iter> bool std::__cxx11::operator<(const 
   std::__cxx11::sub_match<_BiIter>&, const typename 
   std::iterator_traits<_Iter>::value_type&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/regex.h:1429:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::sub_match<_BiIter>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/forward_list:38:0,
                 from c.cc:19:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/forward_list.h:1376:5: note: candidate:
   template<class _Tp, class _Alloc> bool std::operator<(const 
   std::forward_list<_Tp, _Alloc>&, const std::forward_list<_Tp, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/forward_list.h:1376:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::forward_list<_Tp, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/set:62:0,
                 from c.cc:14:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_multiset.h:840:5: note: candidate:
   template<class _Key, class _Compare, class _Alloc> bool std::operator<(const 
   std::multiset<_Key, _Compare, _Alloc>&, const std::multiset<_Key, _Compare, 
   _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_multiset.h:840:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::multiset<_Key, _Compare, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/set:61:0,
                 from c.cc:14:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_set.h:857:5: note: candidate: 
   template<class _Key, class _Compare, class _Alloc> bool std::operator<(const 
   std::set<_Key, _Compare, _Alloc>&, const std::set<_Key, _Compare, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_set.h:857:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::set<_Key, _Compare, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/stack:61:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:48,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_stack.h:293:5: note: candidate: 
   template<class _Tp, class _Seq> bool std::operator<(const std::stack<_Tp, 
   _Seq>&, const std::stack<_Tp, _Seq>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_stack.h:293:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::stack<_Tp, _Seq>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/deque:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/stack:60,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:48,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:2238:5: note: candidate:
   template<class _Tp, class _Alloc> bool std::operator<(const std::deque<_Tp, 
   _Alloc>&, const std::deque<_Tp, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:2238:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::deque<_Tp, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/deque:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/stack:60,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:48,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:299:5: note: candidate: 
   template<class _Tp, class _RefL, class _PtrL, class _RefR, class _PtrR> bool 
   std::operator<(const std::_Deque_iterator<_Tp, _Ref, _Ptr>&, const 
   std::_Deque_iterator<_Tp, _RefR, _PtrR>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:299:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::_Deque_iterator<_Tp, _Ref, _Ptr>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/deque:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/stack:60,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:48,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:291:5: note: candidate: 
   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/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_deque.h:291:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::_Deque_iterator<_Tp, _Ref, _Ptr>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:382:5: note: candidate:
   template<class _Tp> bool std::operator<(std::nullptr_t, const 
   std::shared_ptr<_Tp1>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:382:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::__cxx11::list<std::__cxx11::basic_string<char> 
   >::iterator {aka std::_List_iterator<std::__cxx11::basic_string<char> >}' is 
   not derived from 'const std::shared_ptr<_Tp1>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:377:5: note: candidate:
   template<class _Tp> bool std::operator<(const std::shared_ptr<_Tp1>&, 
   std::nullptr_t)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:377:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::shared_ptr<_Tp1>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:368:5: note: candidate:
   template<class _Tp1, class _Tp2> bool std::operator<(const 
   std::shared_ptr<_Tp1>&, const std::shared_ptr<_Tp2>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:368:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::shared_ptr<_Tp1>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1232:5: note: candidate:
   template<class _Tp, __gnu_cxx::_Lock_policy _Lp> bool 
   std::operator<(std::nullptr_t, const std::__shared_ptr<_Tp, _Lp>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1232:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::__cxx11::list<std::__cxx11::basic_string<char> 
   >::iterator {aka std::_List_iterator<std::__cxx11::basic_string<char> >}' is 
   not derived from 'const std::__shared_ptr<_Tp, _Lp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1227:5: note: candidate:
   template<class _Tp, __gnu_cxx::_Lock_policy _Lp> bool std::operator<(const 
   std::__shared_ptr<_Tp, _Lp>&, std::nullptr_t)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1227:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__shared_ptr<_Tp, _Lp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr.h:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/memory:82,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/regex:46,
                 from c.cc:12:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1218:5: note: candidate:
   template<class _Tp1, class _Tp2, __gnu_cxx::_Lock_policy _Lp> bool 
   std::operator<(const std::__shared_ptr<_Tp1, _Lp>&, const 
   std::__shared_ptr<_Tp2, _Lp>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/shared_ptr_base.h:1218:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__shared_ptr<_Tp1, _Lp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/vector:64:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/random.h:34,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/random:49,
                 from c.cc:10:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:1526:5: note: candidate:
   template<class _Tp, class _Alloc> bool std::operator<(const std::vector<_Tp, 
   _Alloc>&, const std::vector<_Tp, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_vector.h:1526:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::vector<_Tp, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/map:62:0,
                 from c.cc:8:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_multimap.h:989:5: note: candidate:
   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/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_multimap.h:989:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::multimap<_Key, _Tp, _Compare, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/map:61:0,
                 from c.cc:8:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_map.h:1324:5: note: candidate: 
   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/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_map.h:1324:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::map<_Key, _Tp, _Compare, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/map:60:0,
                 from c.cc:8:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_tree.h:1292:5: note: candidate: 
   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/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_tree.h:1292:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::_Rb_tree<_Key, _Val, _KeyOfValue, _Compare, 
   _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/list:63:0,
                 from c.cc:6:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_list.h:1887:5: note: candidate: 
   template<class _Tp, class _Alloc> bool std::operator<(const 
   std::__cxx11::list<_Tp, _Alloc>&, const std::__cxx11::list<_Tp, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_list.h:1887:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::list<_Tp, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_conv.h:41:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/locale:43,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:43,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:701:5: note: candidate:
   template<class _Tp, class _Dp> bool std::operator<(std::nullptr_t, const 
   std::unique_ptr<_Tp, _Dp>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:701:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::__cxx11::list<std::__cxx11::basic_string<char> 
   >::iterator {aka std::_List_iterator<std::__cxx11::basic_string<char> >}' is 
   not derived from 'const std::unique_ptr<_Tp, _Dp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_conv.h:41:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/locale:43,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:43,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:695:5: note: candidate:
   template<class _Tp, class _Dp> bool std::operator<(const 
   std::unique_ptr<_Tp, _Dp>&, std::nullptr_t)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:695:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::unique_ptr<_Tp, _Dp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_conv.h:41:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/locale:43,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:43,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:684:5: note: candidate:
   template<class _Tp, class _Dp, class _Up, class _Ep> bool 
   std::operator<(const std::unique_ptr<_Tp, _Dp>&, const std::unique_ptr<_Up, 
   _Ep>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:684:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::unique_ptr<_Tp, _Dp>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:37:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_conv.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/locale:43,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:43,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/tuple:1350:5: note: candidate: 
   template<class ... _TElements, class ... _UElements> constexpr bool 
   std::operator<(const std::tuple<_Elements ...>&, const std::tuple<_Elements 
   ...>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/tuple:1350:5: note:   template argument 
   deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::tuple<_Elements ...>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/tuple:39:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/unique_ptr.h:37,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_conv.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/locale:43,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:43,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/array:251:5: note: candidate: 
   template<class _Tp, long unsigned int _Nm> bool std::operator<(const 
   std::array<_Tp, _Nm>&, const std::array<_Tp, _Nm>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/array:251:5: note:   template argument 
   deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::array<_Tp, _Nm>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:46:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/system_error:274:3: note: candidate: 
   bool std::operator<(const std::error_condition&, const 
   std::error_condition&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/system_error:274:3: note:   no known 
   conversion for argument 1 from 
   'std::_List_iterator<std::__cxx11::basic_string<char> >' to 'const 
   std::error_condition&'
/usr/local/gcc-6.3.0/include/c++/6.3.0/system_error:200:3: note: candidate: 
   bool std::operator<(const std::error_code&, const std::error_code&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/system_error:200:3: note:   no known 
   conversion for argument 1 from 
   'std::_List_iterator<std::__cxx11::basic_string<char> >' to 'const 
   std::error_code&'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/string:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_classes.h:40,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5160:5: note: candidate:
   template<class _CharT, class _Traits, class _Alloc> bool 
   std::operator<(const _CharT*, const std::__cxx11::basic_string<_CharT, 
   _Traits, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5160:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   mismatched types 'const _CharT*' and 
   'std::_List_iterator<std::__cxx11::basic_string<char> >'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/string:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_classes.h:40,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5148:5: note: candidate:
   template<class _CharT, class _Traits, class _Alloc> bool 
   std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, 
   const _CharT*)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5148:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/string:52:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/locale_classes.h:40,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/ios_base.h:41,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/iomanip:40,
                 from c.cc:2:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5135:5: note: candidate:
   template<class _CharT, class _Traits, class _Alloc> bool 
   std::operator<(const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&, 
   const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/basic_string.h:5135:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::__cxx11::basic_string<_CharT, _Traits, _Alloc>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:1143:5: note: candidate:
   template<class _Iterator> bool std::operator<(const 
   std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorL>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:1143:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::move_iterator<_IteratorL>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:1137:5: note: candidate:
   template<class _IteratorL, class _IteratorR> bool std::operator<(const 
   std::move_iterator<_IteratorL>&, const std::move_iterator<_IteratorR>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:1137:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::move_iterator<_IteratorL>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:355:5: note: candidate:
   template<class _IteratorL, class _IteratorR> bool std::operator<(const 
   std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_IteratorR>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:355:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::reverse_iterator<_Iterator>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:298:5: note: candidate:
   template<class _Iterator> bool std::operator<(const 
   std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:298:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::reverse_iterator<_Iterator>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/utility:70:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:60,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_pair.h:437:5: note: candidate: 
   template<class _T1, class _T2> constexpr bool std::operator<(const 
   std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_pair.h:437:5: note:   template 
   argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const std::pair<_T1, _T2>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:892:5: note: candidate:
   template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const 
   __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const 
   __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:892:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const __gnu_cxx::__normal_iterator<_Iterator, _Container>'
In file included from /usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_algobase.h:67:0,
                 from /usr/local/gcc-6.3.0/include/c++/6.3.0/algorithm:61,
                 from c.cc:1:
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:885:5: note: candidate:
   template<class _IteratorL, class _IteratorR, class _Container> bool 
   __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, 
   _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
/usr/local/gcc-6.3.0/include/c++/6.3.0/bits/stl_iterator.h:885:5: note:   
   template argument deduction/substitution failed:
c.cc:2:38: note:   'std::_List_iterator<std::__cxx11::basic_string<char> >' is 
   not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, 
   _Container>'

Comparisons, part two

This will work:

list<string> l = {"kappa", "alpha", "gamma"};
for (auto it = l.begin(); it != l.end(); ++it)
    cout << *it << ' ';
kappa alpha gamma 

set<>::iterator is a BidirectionalIterator, not a RandomAccessIterator, and so < isn’t defined. What would it compare? The addresses of the linked list nodes? That’s not useful.

Constructors

All containers accept a pair of iterators as ctor arguments. These do not have to be iterators for the same type of container.

string today = "2018-01-22";
cout << today << '\n';
multiset<char> ms(today.begin(), today.end());
for (char c : ms)
    cout << c;
2018-01-22
--00112228
string today = "2018-01-22";
cout << today << '\n';
string year(today.begin(), today.begin()+4);
for (char c : year)
    cout << c;
2018-01-22
2018

Modified: 2017-12-21T20:08

User: Guest

Check: HTML CSS
Edit History Source
Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA
© 2015 Colorado State University
CS Building