ericniebler / range-v3

Range library for C++14/17/20, basis for C++20's std::ranges
Other
4.06k stars 437 forks source link

Is this library suggests a way to make a view with inserted element? #1742

Closed denzor200 closed 1 year ago

denzor200 commented 1 year ago

Can i write some code like this?

#include <range/v3/all.hpp>
#include <string>
#include <iostream>
int main()
{
        auto op = [](auto & input, auto & ins)
        {
            return input | ranges::views::push_back(ins)
                         | ranges::views::push_front(ins);
        };
        std::string input{"foobarbaxbat"};
        char insert{'|'};
        auto rng = op(input, insert);
        for (const char c : rng) std::cout << c;
        std::cout << std::endl;
        // Expected output:
        //   |foobarbaxbat|
        //   
}

I know this library has push_back action, but i can't find suitable view.

JohelEGP commented 1 year ago

views::concat(views::single('|'), input, views::single('|')).

denzor200 commented 1 year ago

@JohelEGP many thanks for the answer!