Show Lecture.OtherIterators as a slide show.
<iterator>
.
ostream_iterator
The usual way of writing things to cout:
cout << "Always"; cout << " test"; cout << " your code.";
Always test your code.
The ostream_iterator
way:
ostream_iterator<string> it(cout); *it++ = "Always"; *it++ = " test"; *it++ = " your code.";
Always test your code.
ostream_iterator
with containersThe usual way of writing things to cout:
vector<string> vs = {"Always", "test", "your code."}; for (auto v : vs) cout << v << ' ';
Always test your code.
The ostream_iterator
way:
vector<string> vs = {"Always", "test", "your code."}; ostream_iterator<string> it(cout, " "); copy(vs.begin(), vs.end(), it);
Always test your code.
istream_iterator
Consider this file:
$ cat ~cs253/pub/ducks Huey (red) Dewey (blue) Louie (green)
istream_iterator
The usual way to read all strings from the file:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); string s; while (in >> s) cout << "☆☆☆ " << s << '\n';
☆☆☆ Huey ☆☆☆ (red) ☆☆☆ Dewey ☆☆☆ (blue) ☆☆☆ Louie ☆☆☆ (green)
istream_iterator
Using istream_iterator
:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); string s; istream_iterator<string> it(in), eos; while (it != eos) cout << "☆☆☆ " << *it++ << '\n';
☆☆☆ Huey ☆☆☆ (red) ☆☆☆ Dewey ☆☆☆ (blue) ☆☆☆ Louie ☆☆☆ (green)
istream_iterator
Using copy
with an istream_iterator
and an ostream_iterator
:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); istream_iterator<string> it(in), eos; ostream_iterator<string> out(cout, "\n"); copy(it, eos, out);
Huey (red) Dewey (blue) Louie (green)
istream_iterator
Similarly, but displaying ★ after each string
:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); istream_iterator<string> it(in), eos; ostream_iterator<string> out(cout, "★"); copy(it, eos, out);
Huey★(red)★Dewey★(blue)★Louie★(green)★
istream_iterator
Similarly, but working with char
, not string
:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); istream_iterator<char> it(in), eos; ostream_iterator<char> out(cout, "★"); copy(it, eos, out);
H★u★e★y★(★r★e★d★)★D★e★w★e★y★(★b★l★u★e★)★L★o★u★i★e★(★g★r★e★e★n★)★
istream_iterator
Similarly, but working with double
, not char
:
const string home = getpwnam("cs253")->pw_dir; ifstream in(home + "/pub/ducks"); istream_iterator<double> it(in), eos; ostream_iterator<double> out(cout, "★"); copy(it, eos, out);
insert_iterator
The insert_iterator
inserts into a container by calling .insert()
:
set<int> source = {678, 901, 234, 567, 890, 123}; list<int> destination = {1, 2, 3, 4, 5}; auto it = destination.begin(); advance(it, 3); // Why not it+=3? insert_iterator<list<int>> insert_it(destination,it); copy(source.begin(), source.end(), insert_it); for (auto v : destination) cout << v << ' ';
1 2 3 123 234 567 678 890 901 4 5
Why did the order change?
front_insert_iterator
front_insert_iterator
is like insert_iterator
,
except that it inserts at the front of the container:
unordered_set<int> source = {11111, 22222, 33333, 44444, 55555}; deque<int> destination = {1, 2, 3, 4, 5}; front_insert_iterator<deque<int>> fi(destination); copy(source.begin(), source.end(), fi); for (auto v : destination) cout << v << ' ';
11111 22222 33333 44444 55555 1 2 3 4 5
back_insert_iterator
Same thing, other side:
forward_list<char> source = {'b', 'a', 'r'}; string destination = "Foo"; back_insert_iterator<string> bi(destination); copy(source.begin(), source.end(), bi); cout << destination << '\n';
Foobar
The convenience functions front_inserter()
and back_inserter()
return front_insert_iterator<T>
or back_insert_iterator<T>
of the appropriate type:
unordered_set<int> source = {11111, 22222, 33333, 44444, 55555}; deque<int> destination = {1, 2, 3, 4, 5}; auto fi = front_inserter(destination); copy(source.begin(), source.end(), fi); for (auto v : destination) cout << v << ' ';
11111 22222 33333 44444 55555 1 2 3 4 5
forward_list<char> source = {'b', 'a', 'r'}; string destination = "Foo"; auto bi = back_inserter(destination); copy(source.begin(), source.end(), bi); cout << destination << '\n';
Foobar