Sign in with Google to keep private notes

Iterators, in C++

  1. prefer using iterators over indexing because for data structrues like std::list using indexing would incur O(n squared) time complexity. Keeping an iterating is preferred.

  2. you can use .cbegin() or .cend() to make use of iterators with a const quality

  3. RAM iterators allow all operations. But associative iterators to sets or maps or lists do not have some of those operations (like < or >). Of them there are bidirectional iterators and forward iterators.

The weirdos can =, *, ==, !=, ++. The bidirectional iterators can use --. The forward iterators can't. Yes this implies < doens't work for the non-RAM iterators so you have to resort to using !=.

Of the RAM iterators, all of them except the deque's elements are contiguous. This allows easy casting and other cache friendliness huehuehuehue

std::sort will only work if the iterators you provide are random access iterators

the [) range convention is adopted so that if the collection is empty the returned iterator will be the .end() iterator, same if there was no iterator.

Pitfalls

Iterators pointing to positons in vectors before resizes will cause UB.

Mostl STL algorithms don't allow destruction so calling remove on a std::list (underlying linked list) will attempt to backfill removed elements while keeping the list size unchanged. And std::remove gives you the index of the new end of the array.

There are some exceptions to the No Removal rule, and back_inserter is one of them.

vector<int> d2;

std::transform(v1.begin(), v1.end()
               std::back_inserter(d2),
               my_mapping_function);

If you call transform on an empty array without a back_inserter it throws an error.

Everything that behaves like an iterator is an iterator though, so using std::back_inserter allows you to return an iterator while push_backing at the same time, and cheat your empty vector array.

for (int elem : std::ranges::subrange(coll.begin(), newEnd)) { // elems til new end
    std::cout << elem << ' ';
}
std::cout << '\n';
std::list<int> coll{6, 5, 4, 3, 2, 1, 1, 2, 3, 4, 5, 6};

// print all elements not having value 3:
auto not3 = [] (const auto& elem) {
    return elem != 3;
};

for (int elem : coll | std::views::filter(not3)) {
    std::cout << elem << ' ';
}
std::cout << '\n';

You can make use of the views library.

std::vector<int> coll{0, 8, 15, 47, 11, 42};

auto greater40 = [] (const auto& elem) {
    return elem > 40;
};


auto v1 = coll | std::views::filter(greater40); //47, 42
//means, to coll
auto pos = v1.begin();
while (pos != v1.end()) {
    cout << *pos << endl;
    ++pos;
}

Filter caches the first iterator so just be careful when you modify elements of a filter view. It probably triggers undefined behaviour (if the resulting value nolonger satisfies the filter predicate, so they say).

Single pass will be fine.

Other stuff

void print(const auto& coll) {
    for (const auto& elem : coll) {
        std::cout << elem << ' ';
    }
    std::cout << '\n';
}

std::vector<int> vec{0, 8, 15, 47, 11, 42, 1};
std::list<int> lst{0, 8, 15, 47, 11, 42, 1};

print(vec);
print(lst);

auto gt40 = [](const auto& elem) { return elem > 40; };

for (const auto& elem : vec | std::views::filter(gt40)) {  // OK
    std::cout << elem << ' ';
}

print(vec | std::views::filter(gt40));  // ERROR

print(vec | std::views::drop(3));       // OK
print(lst | std::views::drop(3));       // ERROR

The problem is const auto& in print(). Some views cannot be iterated when they are const, because they need to cache iterator positions internally. filter_view always has a non-const begin(), so passing it to print() fails. drop_view can provide a const begin() for random-access, sized ranges like vector, which lets it calculate the starting position directly. However, list is not random-access, so its drop_view needs to cache the starting position and cannot be iterated through a const reference.

The fix: Change print(const auto& coll) to print(auto&& coll). This accepts containers and temporary views without unnecessarily making those views const.

Input iterators can only be read once. I neither know nor care yet. They are not equality preserving. You can safely read the element only once. They're some kind of seperate category.

Articles to read next