Is there a way in Range-v3 to prepend or append an element to a range/view?

Viewed 208

Range-v3 has ranges::views::drop and ranges::views::drop_last to remove elements from the front or the back of a view.

Does it offer a similar functions to prepend/append elements to a view?

For now, the shortest way I've found is to concat the range/container with a iota or with a single:

#include <assert.h>
#include <range/v3/view/iota.hpp>
#include <range/v3/view/concat.hpp>
#include <range/v3/to_container.hpp>

using namespace ranges;
using namespace views;
int main() {
    std::vector<int> v{1,2,3};

    auto wi = concat(iota(0,1),v);
    assert(((wi | to_vector) == std::vector<int>{0,1,2,3}));

    auto ws = ranges::views::concat(single(0), v);
    assert(((ws | to_vector) == std::vector<int>{0,1,2,3}));
}
0 Answers
Related