C++ LINQ-like iterator operations

后端 未结 8 968
执念已碎
执念已碎 2021-02-13 13:40

Having been tainted by Linq, I\'m reluctant to give it up. However, for some things I just need to use C++.

The real strength of linq as a linq-consumer (i.e. to me) li

相关标签:
8条回答
  • 2021-02-13 14:21

    The Abseil library has many https://github.com/abseil/abseil-cpp/blob/master/absl/algorithm/container.h has many container functions: c_all_of, c_any_of, c_none_of, c_find, c_count, c_count_if, c_replace_copy, c_unique_copy, c_min_element, c_max_element, c_accumulate

    0 讨论(0)
  • 2021-02-13 14:23

    Here is another alternative that is simply a wrapper around boost and stl algorithms, and thus you get all the performance benefits of those implementations.

    It works like this:

    std::vector<int> xs;
    auto count = from(xs)
       .select([](int x){return x*x;})
       .where([](int x){return x > 16;})
       .count();
    auto xs2 = from(xs)
       .select([](int x){return x*x;})
       .to<std::vector<int>>();
    

    Note that some methods return a proxy for empty ranges, e.g.

    std::vector<int> xs;
    auto max = from(xs)
       .select([](int x){return x*x;})
       .where([](int x){return x > 16;})
       .max()
       .value_or(0);
    
    0 讨论(0)
提交回复
热议问题