Beginner's Guide
    First Steps
    Input & Output
    Custom Types – Part 1
    Diagnostics
    Standard Library – Part 1
    Function Objects
    Standard Library – Part 2
    Code Organization
    Custom Types – Part 2
    Generic Programming
    Memory Management
    Software Design Basics

    Special Standard Iterators Special Iterators Special Iterators

    std::copy(@source_begin, @source_end, @target_begin)  @target_end
    std::ranges::copy(source_range, @target_begin)  {@source_end, @target_end}  C++20

    Special input/output iterators cannot be used with algorithms that require forward, bidirectional or random access iterators.

    Container Manipulators Container

    • can be used to insert new elements into containers
    • (only) useful if number of inserted elements not known in advance
    • avoid in performance-critical code:
      reserve container memory in advance and use a normal iterator instead

    std::insert_iterator<Container> insert_iterator inserter

    • output iterator: data sink / target
    • single-pass: each position can only be accessed once
    • stores reference to container and insert position on construction
    • *it = value calls .insert(position, value) on a container
    • * and ++ are no-ops, * just returns a reference to the iterator itself
    std::inserter(container, position) 
     → std::insert_iterator<ContainerType>{container, position}
    std::vector<int> v {4, 8, 2};
    std::array<int,2> a {5, 6};
    copy(begin(a), end(a), 
         inserter(v, begin(v)+2));
    
    std::insert_iterator< std::vector<int>> it {v,begin(v)+1}; *it = 1;
    v: 
    • 4
    • 8
    • 2
    a:
    • 5
    • 6
    v:
    • 4
    • 8
    • 5
    • 6
    • 2

    v:
    • 4
    • 1
    • 8
    • 5
    • 6
    • 2

    std::back_insert_iterator<Container> back_insert_iterator back_inserter

    • output iterator: data sink / target
    • single-pass: each position can only be accessed once
    • stores reference to container on construction
    • *it = value calls .push_back(value) on a container
    • * and ++ are no-ops, * just returns a reference to the iterator itself
    std::back_inserter(container)
     → std::back_insert_iterator<ContainerType>{container}
    std::vector<int> v {4, 8, 2};
    std::array<int,2> a {5, 6};
    copy(begin(a), end(a), 
         back_inserter(v));
    
    std::back_insert_iterator< std::vector<int>> it {v}; *it = 1;
    v: 
    • 4
    • 8
    • 2
    a:
    • 5
    • 6
    v:
    • 4
    • 8
    • 2
    • 5
    • 6

    v:
    • 4
    • 8
    • 2
    • 5
    • 6
    • 1

    std::front_insert_iterator<Container> front_insert_iterator front_inserter

    • output iterator: data sink / target
    • single-pass: each position can only be accessed once
    • stores reference to container on construction
    • *it = value calls .push_front(value) on a container
    • * and ++ are no-ops, * just returns a reference to the iterator itself
    std::front_inserter(container)
    → std::front_insert_iterator<ContainerType>{container}
    std::deque<int> d {4, 8, 2};
    std::array<int,2> a {5, 6};
    copy(begin(a), end(a), 
         front_inserter(d));
    
    std::front_insert_iterator< std::deque<int>> it {d}; *it = 1;
    d: 
    • 4
    • 8
    • 2
    a:
    • 5
    • 6
    d:
    • 6
    • 5
    • 4
    • 8
    • 2

    d:
    • 1
    • 6
    • 5
    • 4
    • 8
    • 2

    I/O Stream Iterators I/O Streams

    • allow to use streams as sources or targets for standard algorithms
    • should be avoided in performance-critical code

    std::ostream_iterator<ValueType> ostream_iterator ostream_iterator

    • output iterator: data sink / target
    • single-pass: each position can only be accessed once
    • stores reference to output stream on construction
    • *it = value puts value into stream
    • * and ++ are no-ops, * just returns a reference to the iterator itself
    std::vector<int> v {1,2,3,4};
    copy(begin(v), end(v), 
         std::ostream_iterator<int>{cout});
    copy(begin(v), end(v), 
         std::ostream_iterator<int>{cout," "});
    copy(begin(v), end(v), 
         std::ostream_iterator<int>{cout,"/"});
    
    
    1234
    
    1 2 3 4
    
    1/2/3/4

    std::istream_iterator<ValueType> istream_iterator istream_iterator

    • input iterator: data source
    • single-pass: each position can only be accessed once
    • stores reference to input stream on construction
    • *it gets current value from stream
    • ++it advances stream to next value
    • an istream_iterator constructed without argument can be used to indicate end-of-range
    #include <fstream>
    std::ifstream is {"numbers.txt"};
    std::vector<int> v;
    copy(std::istream_iterator<int>{is},  // src begin
         std::istream_iterator<int>{},    // src end
         back_inserter(v) );              // tgt begin
    #include <sstream>
    std::istringstream str {"8 7 3 5 4"};
    std::vector<int> w;
    copy(std::istream_iterator<int>{str},  // src begin
         std::istream_iterator<int>{},     // src end
         back_inserter(w) );               // tgt begin
    // w: 
    • 8
    • 7
    • 3
    • 5
    • 4

    Standard Algorithm Compatibility Algorithm Compatibility Algorithms

    The following tables list standard algorithms that are compatible with the special iterators presented above.

    Since the special iterators are only input or output iterators they cannot be used with algorithms that require forward, bidirectional or random access iterators.