Sobes.tech
Junior — Senior

How to make UserCollection suitable for range-based loop without access to the field

livecode

Task condition

It is necessary to modify the structure UserCollection so that it can be used in a range-based for loop without accessing the internal array.

struct UserCollection {
    std::vector<int> arr;

    // what members/operators to add?
};

void rangeFunc() {
    UserCollection col;
    for (const auto& item : col) {
        // ...
    }
}

It is required to add the necessary type aliases, begin() and end() methods (or corresponding free functions) to the structure, which will return iterators of the arr container, so that the loop for (const auto& item : col) works directly.