为什么没有排序(v)在C ++? [英] why no sort(v) in C++?
问题描述
我总是想知道为什么没有
I always wondered why there is no
sort(v);// same as std::sort(v.begin(),v.end())
$ b剪辑,其中扬声器说,概念是必需的,但我不明白为什么。
BTW我试过这个(在VS 11),它工作niceli从我可以看到。
If I recall correctly long time ago I saw a boostcon clip where speaker said that concepts are required for this, but I dont see why. BTW I tried this (in VS 11) and it works niceli from what I can see.
template <typename Container>
void sortfx(Container& c)
{
std::sort(c.begin(),c.end());
}
int main()
{
std::vector<double> v;
//std::list<double> v; this causes compile errors
v.push_back(1701);
v.push_back(1729);
v.push_back(74656);
v.push_back(2063);
sortfx(v);
assert(std::is_sorted(begin(v),end(v)));
}
编辑:
Bjarne自己解释了概念,以sort为例:)
https://www.informit.com/articles/article.aspx?p=2080042&WT.rss_f=Article&WT.rss_a=An%20Interview%20with%20Bjarne %20Stroustrup& WT.rss_ev = a
推荐答案
这不是 std :: sort (v)
- > std :: sort(v.begin(),v.end())
替换排序函数为比较添加一个附加参数 - std :: sort(v.begin(),v.end(),compare)
。
It's not the std::sort(v)
-> std::sort(v.begin(), v.end())
expansion that would need concepts, but the alternate sort function taking an additional parameter for the comparison - std::sort(v.begin(), v.end(), compare)
.
如果你有一个调用 std :: sort(v,compare)
,实现将其与 std :: sort(start,end)
区分开。
If you have a call std::sort(v, compare)
, the implementation would need concepts to distinguish it from std::sort(start, end)
for a non-container.
< algorithm>
标题中充满了这种问题的模板。
The <algorithm>
header is full of templates with this kind of problem.
这篇关于为什么没有排序(v)在C ++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!