iterator1.cppFor_Each() using pointers to array or vector elements
For_Each() using begin() & end()
For vectors, they return the appropriate pointers
But the same code works if we replace vector with list
list::begin() can’t just return a pointer – For_Each() does
++b, which is not the right thing to do for linked listIterators
for (list<int>::iterator i = v.begin(); i != v.end(); ++i) {
begin() returns an iterator pointing at the first elementend() returns an iterator pointing at one PAST the last element[ b, e )for (list<int>::const_iterator i = v.begin(); i != v.end(); ++i) {
T&const T&iterator and range-for loop
begin() & end()Subtleties in For_Each() implementation
(b < e) instead of (b != e)?
b++ instead of ++b?
consider how one would implement prefix and postfix increments:
iterator& operator(); // prefix increment
iterator operator(int); // postfix increment
iterator2.cppstd::copy()
()-initializer vs. {}-initializer
Try the following two differnt initializers:
vector<int> v2(5);
vector<int> v2{5};
For STL containers, the ()-initializer syntax is for sizes and the {}-initializer syntax is for a list of elements
vector( std::initializer_list<T> init )
When an object is initialized with a brace-enclosed initializer list, and a constructor takes a std::initializer_list parameter, a std::initializer_list object is automatically constructed
std::initializer_list<T> is a lightweight viewer object that provides
access to an array of the initializers, which may have been placed
in read-only memory by the compiler
std::initializer_list can be implemented with a pointer and the length
of the underlying array; copying a std::initializer_list does not copy
the underlying array
std::initializer_list provides begin() and end()
Iterator that calls push_back()
IteratorThatPushes class code walk-thru
vector<int> v2;
copy(v1.begin(), v1.end(), IteratorThatPushes<vector<int>>{v2});
copy(v1.begin(), v1.end(), IteratorThatPushes{v2});
STL version:
copy(v1.begin(), v1.end(), back_inserter(v2));
ostream_iterator
Iterator that writes to any ostream object
ostream_iterator<int> oi {cout, "\n"};
copy(v2.begin(), v2.end(), oi);
istream_iterator
Iterator that reads from any istream object
vector<int> v2;
istream_iterator<int> iib {cin};
istream_iterator<int> iie {};
copy(iib, iie, back_inserter(v2));
reverse(v2.begin(), v2.end());
Iterator Categories
Iterator categories are not concrete types. They are defined conceptually by their capabilities – i.e., by the set of operations that they support.
LegacyInputIterator: supports reading and single-pass increment
Iterator Concepts (since C++20)
C++20 turned the idea of these capabilities into a laguage feature called “Concepts”
Iterator Categories were reworked into a similar but slightly different taxonomy called Iterator Concepts.
The old iterator category names were prepended with “Legacy”.