Composability of STL algorithms

后端 未结 4 1307
星月不相逢
星月不相逢 2021-02-02 06:44

The STL algorithms are a pretty useful thing in C++. But one thing that kind of irks me is that they seem to lack composability.

For example, let\'s say I have a v

4条回答
  •  旧巷少年郎
    2021-02-02 07:34

    Back in 2000, the problem was already noted. Gary Powell and Martin Weiser came up with a "view" concept, and coined the name "View Template Library". It didn't take off then but the idea makes sense. A "view" adaptor essentially applies an on-the-fly transform. For instance, it can adapt the value_type.

    The concept probably should be readdressed now we have C++0x. We've made quite some progress in generic programming since 2000.

    For example, let's use the vector> to vector example. That could be quite simple:

    std::vector> values = GetValues();
    vtl2::view v (values, [](std::pair p) { return p.first }); 
    std::vector result(view.begin(), view.end());
    

    Or, using the boost::bind techniques, even simpler:

    std::vector> values = GetValues();
    vtl2::view v (values, &std::pair::first); 
    std::vector result(view.begin(), view.end());
    

提交回复
热议问题