std::for_each working on more than one range of iterators

前端 未结 3 400
心在旅途
心在旅途 2020-12-17 18:31

The lambda notation has made stl algorithms more accessible. I am still learning to decide when it\'s useful and when to fall back to good old fashioned for-loops. Often, it

3条回答
  •  时光取名叫无心
    2020-12-17 18:35

    After reading up on boost::zip_iterator and boost::iterator_range as suggested by some answers, I came across the extension algorithms in boost::range, and found the exact parallel of the algorithm I wrote for two ranges, but with boost ranges.

    A working code for the example would be

    #include 
    
    template
    void foo_two_ranges(vector& data, vector& prop) {
        auto rng1 = boost::make_iterator_range(data.begin(), data.end());
        auto rng2 = boost::make_iterator_range(prop.begin(), prop.end());
        boost::for_each(rng1, rng2, [](Data& d, Property& p) {
            if (p.SomePropertySatistfied()) {
                d.DoSomething();
            }
        });
    }
    

    Some wrappers and utility functions, similar in mind to what @Jonathan Wakely suggested, can make this even more usable.

提交回复
热议问题