我一直想知道为什么没有
sort(v);// same as std::sort(v.begin(),v.end())
如果我没记错的话,很久以前我看到过一个 boostcon 剪辑,演讲者说这个需要概念,但我不明白为什么。
顺便说一句,我尝试了这个(在 VS 11 中),从我所看到的来看,它工作得很好。
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 本人以排序为例解释了这些概念:)
这不是std::sort(v)
-> std::sort(v.begin(), v.end())
需要概念的扩展,但替代排序函数采用附加参数进行比较 -std::sort(v.begin(), v.end(), compare)
.
如果您有电话std::sort(v, compare)
、实施would需要概念来区分它std::sort(start, end)
对于非容器。
The <algorithm>
标题中充满了此类问题的模板。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)