range-v3

Why can't I reverse a split-range using range-v3?

梦想与她 提交于 2020-01-03 18:19:07
问题 I want to split, reverse, and then join a string using range-v3. However, code below won't compile. #include <range/v3/all.hpp> #include <iostream> using namespace ranges; int main(int argc, char *argv[]) { auto str = std::string("abc.def.ghi"); auto sv = str | view::split('.') | view::reverse | view::join('.'); std::cout<<sv; return 0; } Compiler output: error: invalid operands to binary expression ('decltype(pipeable_access::impl<view<reverse_fn> >::pipe(static_cast<ranges::v3::split_view

Unzip in C++ Range-v3 library

偶尔善良 提交于 2019-12-23 12:38:51
问题 Is it possible to unzip previously zipped vectors using the C++ Range-v3 library? I would expect it to behave similarly to Haskell's unzip function or Python's zip(*list). It would be convenient, for instance, when sorting a vector by values of another vector: using namespace ranges; std::vector<std::string> names {"john", "bob", "alice"}; std::vector<int> ages {32, 19, 35}; // zip names and ages auto zipped = view::zip(names, ages); // sort the zip by age sort(zipped, [](auto &&a, auto &&b)

How to concat two existing ranges::view?

余生颓废 提交于 2019-12-23 02:39:29
问题 I want to use an existing view for concatenation. In code: auto rng = view::empty<vector<int>>(); for(int i{0}; i < 5; ++i) { vector<int> const & v{foo()}; // returns a reference rng |= view::concat(v); // doesn't compile - error: no viable overloaded '|=' }; In other words - how can I create a view to multiple vectors whose number is not known until runtime? 回答1: You can't compose views this way. Concatenating a view yields an object with a different type. You can't assign it back to the

Why is ranges::ostream_iterator default-constructible?

独自空忆成欢 提交于 2019-12-22 05:13:29
问题 This question follows a discussion in the comments here. In Eric Niebler's ranges-v3 library (which is sort-of becoming part of the standard for C++20), ranges::ostream_iterator is default-constructible - without an ostream. How come? I thought that "dummy" construction with effective construction later is an anti-pattern in C++, a wart we are gradually getting rid of. std::ostream iterator can only be constructed with a stream (for now - before C++20). And it's not as though we can do

Range-v3: Use view_facade to provide both const and non-const iterators

为君一笑 提交于 2019-12-21 09:37:18
问题 I am having trouble using view_facade (from range-v3) to create a view that provides both const and non-const access. As an example, I tried modifying the view_facade test (in test/view_facade.cpp) to allow non-const access (by default it only allows const access): struct MyRange : ranges::range_facade<MyRange> { private: friend struct ranges::range_access; std::vector<int> ints_; template <bool isConst> struct cursor { private: using It = typename std::conditional<isConst, std::vector<int>:

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

↘锁芯ラ 提交于 2019-12-18 10:37:40
问题 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

write to a zipped back inserted ranges

孤者浪人 提交于 2019-12-11 11:54:15
问题 A close cousin of this other question, but with back_inserter : #include <range/v3/view.hpp> #include <range/v3/view/zip.hpp> #include <range/v3/utility/iterator.hpp> // ... std::vector< std::tuple<int, std::string, double> > const data{ {1,"a", 3.14}, {2,"b", 42.0}, {3,"c"} }; std::vector<int> vi; std::vector<std::string> vs; std::vector<double> vd; using namespace ranges; copy(data, view::zip( back_inserter(vi), back_inserter(vs), back_inserter(vd)) ); This is obviously an error because

writable zip ranges are not possible?

那年仲夏 提交于 2019-12-11 11:05:08
问题 The following is failing: #include <range/v3/view.hpp> #include <range/v3/view/zip.hpp> #include <range/v3/utility/iterator.hpp> // ... std::vector< std::tuple<int, std::string> > const data{ {1,"a"}, {2,"b"}, {3,"c"} }; std::vector<int> vi(data.size()); std::vector<std::string> vs(data.size()); using namespace ranges; copy(data, view::zip(vi,vs) ); // error clang says No matching function for call to object of type 'const ranges::v3::with_braced_init_args<ranges::v3::copy_fn>' Assuming this

How to create a cartesian product range from filtered data?

匆匆过客 提交于 2019-12-11 03:36:19
问题 I am trying to create a cartesian-product range out of smaller ranges. I thought ranges::v3::view::cartesian_product would work, but somehow it doesn't. If I try to create a cartesian product using containers directly, I have no problem. The following compiles: #include <vector> #include <range/v3/view/cartesian_product.hpp> int main() { std::vector<int> data1{1,5,2,7,6,3,4,8,9,0}; std::vector<int> data2{1,5,2,7,6,3,4,8,9,0}; auto range = ranges::v3::view::cartesian_product(data1, data2); }

Non-pointer-operand error when dereferencing an iterator into a temporary range

我们两清 提交于 2019-12-10 16:29:41
问题 Using auto empty_line = [](auto& str){ return str.size() == 0; }; we can do this: auto line_range_with_first_non_empty = ranges::view::drop_while(ranges::getlines(std::cin),empty_line); auto input1 = std::stoi(*line_range_with_first_non_empty.begin()); and we can also do this: auto line_range2 = ranges::getlines(std::cin); auto iter2 = ranges::find_if_not(line_range2,empty_line); auto input2 = std::stoi(*iter2); Unfortunately, when I try to shorten version above into: auto iter3 = ranges: