vendredi 24 juillet 2020

Insert a vector inside another vector without using the former vector's begin and end (C++20)?

Currently I am inserting a vector inside another like this:

#include <vector>

int main(const int argc, const char** argv)
{
    std::vector<int> old_elements{1, 2, 3};

    std::vector<int> elements{4, 5, 6};
    elements.insert(elements.end(), old_elements.begin(), old_elements.end());
}

What I want to do is something like this

std::vector<int> get_old_elements()
{
    return std::vector<int>{1, 2, 3};
}

int main(const int argc, const char** argv)
{
    std::vector<int> elements{4, 5, 6};
    elements.insert(elements.end(), get_old_elements());
}

So I cannot call begin and end without first storing the temporary result. Is there a way to get this done? During one of the cppcon talks on ranges, I heard it being said that it might be end of begin and end. So I guess there could be some way to achieve something like this with ranges library.

Aucun commentaire:

Enregistrer un commentaire