why no sort(v) in C++?

前端 未结 6 1172
感动是毒
感动是毒 2021-01-17 15:15

I always wondered why there is no

sort(v);// same as std::sort(v.begin(),v.end())

If I recall correctly long time ago I saw a boostcon cli

6条回答
  •  我在风中等你
    2021-01-17 15:48

    Concepts aren't required for this -- but (as they were proposed during C++11 standardization) they would have made it fairly easy to implement this.

    As it stands right now, you could do this by providing a couple of extra overloads (or possibly explicit specializations) for std::sort. The problem, of course, is that std::sort isn't the only algorithm, so you'd undoubtedly want to do the same for lots of other algorithms as well (nearly all of them, most likely).

    Concepts (specifically, concept maps, if memory serves) would have provided a fairly clean way to provide (equivalents of) all those overloads in a relatively centralized way, so you'd have all those adapters in one place instead of N places (one for each algorithm). Better still, as long as they conformed to the normal conventions, it would work for other algorithms as well.

    Quite a few people currently believe ranges are the way this should be handled -- that algorithms should operate on ranges, and any container should define a range (but there should be other ways to define ranges as well). While this is probably good as a general idea, it appears (at least to me) that there's a fair amount of disagreement over the precise details of what should constitute a range, how they should be defined, etc.

    If you really want to explore this, Boost already has a range library that includes range-based versions of most of the standard algorithms (and a number of others as well). At least if memory serves, this includes some adapters to create a range from a container, so things like sort(c) will work without your having to explicitly specify a range or iterators.

提交回复
热议问题