STL algorithms: Why no additional interface for containers (additional to iterator pairs)?

前端 未结 7 608
花落未央
花落未央 2020-11-30 11:31

I\'m wondering why the STL doesn\'t overload their algorithm functions such that I can call them by simply providing a container and not taking the more verbose way to pass

相关标签:
7条回答
  • 2020-11-30 11:52

    Obviously, as other users mentioned, it's a tricky problem, so unfortunately it's been a long time and there's still no solution in the standard library. There are, however, already range libraries available such as Boost::Range and the one in the Adobe Source Libraries that provide not only the simplicity of the interface you describe in your question, but some fancier features as well.

    Your example works perfectly with Boost (we are using namespace boost::range::adaptors below):

    boost::for_each(myVector, doSomething);

    We can also slice myVector quickly and easily:

    boost::for_each(myVector | sliced(10, 20), doSomething)

    We can even zip myVector with another, filter by a predicate, and sample every other element of the resulting pairs in a single, simple statement (this requires that you unpack in doSomethingElse the tuples produced by boost::combined):

    boost::for_each( boost::combined(myVector, myOtherVector) | strided(2), doSomethingElse)

    0 讨论(0)
提交回复
热议问题