# Array Traversal

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

```int a[] = {31, 41, 59, 26, 53, 58};
for (int i=0; i!=6; ++i)
cout << a[i];
```
`314159265358`
• `a[i]` is the same as `*(a+i)`.
• It requires addition and indirection.
• I usually say `i<6`, rather than `i!=6`.
• Similarly, I usually say `i++` rather than `++i`.
• Patience.

# Array Traversal via Pointers

Let’s do it with a pointer:

```int a[] = {31, 41, 59, 26, 53, 58};
for (int *p = &a[0]; p != &a[6]; ++p)
cout << *p;
```
`314159265358`
• That’s a bit faster.
• Instead of `a[i]` (addition+indirection) we have just indirection.
• Not really a radical improvement, however.

# `vector` traversal

You can traverse a `vector` in the same way:

```vector<int> a = {31, 41, 59, 26, 53, 58};
for (int *p = &a[0]; p != &a[6]; ++p)
cout << *p;
```
`314159265358`

or, avoiding the magic number 6:

```vector<int> a = {31, 41, 59, 26, 53, 58};
for (int *p = &a[0]; p != &a[a.size()]; ++p)
cout << *p;
```
`314159265358`

# Methods

```vector<int> a = {31, 41, 59, 26, 53, 58};
for (vector<int>::iterator it = a.begin(); it != a.end(); ++it)
cout << *it;
```
`314159265358`
• `iterator` is a type provided by the templated `vector` class.
• What type is it?
• might be `int *`, might not
• You don’t care what type it is. You know that:
• `*` works on it
• `++` works on it
• you can assign `.begin()` to it
• you can compare it to `.end()`

# `auto` is your friend

This is prettier:

```vector<int> a = {31, 41, 59, 26, 53, 58};
for (auto it = a.begin(); it != a.end(); ++it)
cout << *it;
```
`314159265358`

# `for` loop

This is exactly the same:

```vector<int> a = {31, 41, 59, 26, 53, 58};
for (auto v : a)
cout << v;
```
`314159265358`

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;
```
`ackJ`
```unordered_set<char> u = {'a', 'c', 'k', 'J'};
for (auto it = u.begin(); it != u.end(); ++it)
cout << *it;
```
`Jcka`
```set<char> s = {'a', 'c', 'k', 'J'};
for (auto it = s.begin(); it != s.end(); ++it)
cout << *it;
```
`Jack`

# `iterator` type

• What type is `set<int>::iterator`?
• Who cares?
• It’s not an `int *`, that’s for sure!
• Same for `list<int>::iterator` and `unordered_set<int>::iterator`.
• Most iterator types cannot be a native pointer.
• What happens when you increment a plain pointer?
• It goes to the next memory location.
• Is that what you want for a linked list, a tree, or a hash?
• It must be that `operator++` is overloaded.
• Can’t do that for a plain pointer.
• C++ is extensible, not mutable.
• Therefore, most iterators cannot be plain pointers.

# Iterator classifications

• BidirectionalIterator (`list`, `set`, `unordered_set`)
• `++`, `--`, `==`, `!=`, `*`, `->`, `=`
• RandomAccessIterator (`std::array`, `vector`, `string`)
• `++`, `--`, `+=`int, `-=`int, `+`int, `-`int, iter`-`iter, `==`, `!=`, `<`, `<=`, `>`, `>=`, `*`, `->`, `=`
• ForwardIterator (`forward_list`)
• `++`, `==`, `!=`, `*`, `->`, `=`

# `begin()` & `end()`

• `.begin()` & `.end()` return iterators. Not necessarily pointers.
• `.begin()` is, conceptually, a pointer to the first element of the container.
• `.end()` is, conceptually, a pointer one past the end of the container.
• It’s a half-open interval!
```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: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/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: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/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: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: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/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/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: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/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: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&'
/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&'
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/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/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/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: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/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/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/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: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/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/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/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: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/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/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: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/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/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: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/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/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/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/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/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 `

`list<>::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.

```char hhmmss[] = "230831"; // ISO 8601!
string now(hhmmss, hhmmss+6);
cout << now << '\n';
string mins(now.begin()+2, now.begin()+4);
cout << mins << '\n';
multiset<int> ms(now.begin(), now.end());
for (auto n : ms)
cout << n << ' ';
cout << '\n';
```
```230831
08
48 49 50 51 51 56
```

# `begin()` and `end()` functions

There are also free functions `begin()` and `end()`, which work on arrays (not pointers) and all standard containers:

```char now[] = "11:08pm Wed";
string current(now, now+10);
cout << current << '\n';
```
```11:08pm We
```

Oh dear, I counted wrong. Why am I counting!?

```char now[] = "11:08pm Wed";
string current(begin(now), end(now));
cout << current << '\n';
```
```11:08pm Wed␀
```

User: Guest

Check: HTML CSS
Edit History Source

Modified: 2018-04-24T16:54

Apply to CSU | Contact CSU | Disclaimer | Equal Opportunity
Colorado State University, Fort Collins, CO 80523 USA