Search code examples
c++algorithmiteratorstdreturn-value

return value iterator of std::generate_n, what can you do with it?


Out of curiosity!

What can you do with the returning iterator of std::generate_n?

Return value :

Iterator one past the last element assigned if count > 0, first otherwise.

    std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    auto iterator = std::generate_n(std::back_inserter(v), 10, [&]() {return j++;});
    std::cout << "type: " << typeid(iterator).name() << '\n';
/*
    for (iterator = v.begin(); iterator< v.end(); iterator++){
        std::cout << *iterator << " " << endl;
    }
*/
    for(auto a : v)
        std::cout << a << " ";
    std::cout << std::endl;

Output:

type: St20back_insert_iteratorISt6vectorIiSaIiEEE
1 2 3 4 5 6 7 8 9 10 0 1 2 3 4 5 6 7 8 9 

iterator shows a few member functions, but they are mostly operators.


Solution

  • Suppose you want to generate elements for the first half and then use a different generator for the second half, then you can simply write:

    #include <vector>
    #include <algorithm>
    #include <iostream>
    
    int main() {
        int j = 0;
        std::vector<int> v{1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
        auto iterator = std::generate_n(v.begin(), 5, [&]() {return j++;});
        
        std::generate_n(iterator,5,[&](){ return j;});
        
        
        for (auto i : v){
            std::cout << i << " ";
        }
    }
    

    Without manually calculating the iterator that points to the element after the ones from the first call.

    Output is:

    0 1 2 3 4 5 5 5 5 5 
    

    In your example iterator is just an iterator one past the last element of the vector and brings no big advantage because it is the same as v.end().