range-v3

How do you declare a ranges-v3 view return value?

喜欢而已 提交于 2021-02-19 06:35:11
问题 Currently, I can compose ranges-v3 views like this: auto v = ranges::view::reverse | ranges::view::filter([](int l){return l>5;}); But if I wanted to return v from a function I'd need to know its type. What is the type of a ranges-v3 view? 回答1: Since C++14 you can use auto as the return type of functions and it will get deduced: auto f() { return ranges::view::reverse | ranges::view::filter([](int l){return l>5;}); } // f's return type is the type of the return expression, exactly as is I had

How do you declare a ranges-v3 view return value?

坚强是说给别人听的谎言 提交于 2021-02-19 06:34:05
问题 Currently, I can compose ranges-v3 views like this: auto v = ranges::view::reverse | ranges::view::filter([](int l){return l>5;}); But if I wanted to return v from a function I'd need to know its type. What is the type of a ranges-v3 view? 回答1: Since C++14 you can use auto as the return type of functions and it will get deduced: auto f() { return ranges::view::reverse | ranges::view::filter([](int l){return l>5;}); } // f's return type is the type of the return expression, exactly as is I had

How do I check if ranges:: algorithms like find_if returned a value?

元气小坏坏 提交于 2021-02-10 06:43:49
问题 For example, if I want to find the smallest element of a collection, but only the smallest even element, I'd like to call ranges::min_element with a filtered range like so: using ranges::views::filter; using ranges::min_element; std::vector<int> vals{1,2,3,4,5}; auto minVal = min_element(vals | filter([](int next){return next % 2 == 0;})); How do I check if the returned range is empty, and if not, access the value? The same applies to other range algorithms like ranges::find , ranges::find_if

Unpacking a range of tuples into n-ary function

拥有回忆 提交于 2021-01-27 12:45:50
问题 Suppose I have a range of tuples e.g. coming from the zip function. Do the functions which operate on that range have to be always unary or does there exist some transformation which unpacks the tuple into the function's arguments. Basically, I'd like to do the following: auto r1 = {1, 2, 3, 4}; auto r2 = {'a', 'b', 'c', 'd'}; auto chars = view::zip(r1, r2) | view::transform([](int a, char x) { return x; }); instead of explicitly using std::tie or std::apply. 回答1: It sounds like what you

Combining n vectors into one vector of n-tuples

南笙酒味 提交于 2021-01-27 11:44:35
问题 I'm thinking about a function with signature template<typename ...Ts> std::vector<std::tuple<Ts...>> join_vectors(std::vector<Ts>&&...) { //... }; but probably a more general one accepting any iterable instead of just std::vector would be good. Probably it would have a signature like this? template<template<typename> typename C, typename ...Ts> C<std::tuple<Ts...>> join_vectors(C<Ts>&&...) { // ... }; However, I'm not at this level yet in C++ (despite doing the same in Haskell would be

Range-v3's zip function works with temporaries coming from other Range-v3's functions but not with others

浪尽此生 提交于 2020-12-13 03:29:44
问题 (This is kind of a follow up to this other question.) Original question The following code works just fine #include <boost/range/adaptor/transformed.hpp> #include <cmath> #include <range/v3/view/zip.hpp> #include <string> #include <vector> int main() { auto vec1 = std::vector<int>{1,2,3}; auto vec2 = std::vector<std::string>{"uno","due","tre"}; auto sqrt = [](auto x){ return std::sqrt(x); }; auto vec3 = vec1 | boost::adaptors::transformed(sqrt); for (auto const& i : ranges::views::zip(vec1,

How do I write a range pipeline that uses temporary containers?

一笑奈何 提交于 2020-12-06 12:20:45
问题 I have a third-party function with this signature: std::vector<T> f(T t); I also have an existing potentially infinite range (of the range-v3 sort) of T named src . I want to create a pipeline that maps f to all elements of that range and flattens all the vectors into a single range with all their elements. Instinctively, I would write the following. auto rng = src | view::transform(f) | view::join; However, this won't work, because we cannot create views of temporary containers. How does