CS253

CS253: Software Development with C++

Spring 2017

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

list<string> l = {"kappa", "alpha", "gamma"};
for (auto it = l.begin(); it < l.end(); ++it)
    cout << *it << ' ';
c.cc:2: error: no match for 'operator<' in 'it < 
   l.std::__cxx11::list<std::__cxx11::basic_string<char> >::end()' (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> >'})


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 = "2024-04-27";
cout << today << '\n';
multiset<char> ms(today.begin(), today.end());
for (char c : ms)
    cout << c;
2024-04-27
--00222447


string today = "2024-04-27";
cout << today << '\n';
string year(today.begin(), today.begin()+4);
for (char c : year)
    cout << c;
2024-04-27
2024

Modified: 2017-04-04T17:43

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