C++11 在稳定排序函数中指定迭代器值
我使用下面的代码对矩形进行排序。 在stable_sort函数中,如何指定除boundRect.begin和boundRect.end之外的迭代器值。我想对索引5到10之间的元素进行排序。如何在stable_sort函数中指定这些参数?请引导我C++11 在稳定排序函数中指定迭代器值,c++11,stable-sort,C++11,Stable Sort,我使用下面的代码对矩形进行排序。 在stable_sort函数中,如何指定除boundRect.begin和boundRect.end之外的迭代器值。我想对索引5到10之间的元素进行排序。如何在stable_sort函数中指定这些参数?请引导我 stable_sort( boundRect.begin(), boundRect.end(), compareX_rect ); bool compareX_rect(const Rect & a, const Rect &b) {
stable_sort( boundRect.begin(), boundRect.end(), compareX_rect );
bool compareX_rect(const Rect & a, const Rect &b) {
return a.x <= b.x;
}
stable_sort(boundRect.begin()、boundRect.end()、compareX_rect);
布尔比较器(const rect&a、const rect&b){
返回a.x因为stable\u sort
需要随机访问迭代器,所以可以在迭代器上进行简单的加法:
stable_sort(boundRect.begin()+5, boundRect.begin()+10, /* ... */
除非您使用的是古老的(C++11之前的)编译器,否则您也可以使用lambda表达式进行比较:
stable_sort(boundRect.begin()+5, boundRect.begin()+10,
[](const Rect & a, const Rect &b) { return a.x < b.x; });
stable_排序(boundRect.begin()+5,boundRect.begin()+10,
[](const-Rect&a,const-Rect&b){返回a.x
这不仅更短、更容易阅读,而且通常也会更快(当您只对5个元素进行排序时,这可能并不重要)。由于稳定排序
需要随机访问迭代器,您可以在迭代器上进行简单的加法:
stable_sort(boundRect.begin()+5, boundRect.begin()+10, /* ... */
除非您使用的是古老的(C++11之前的)编译器,否则您也可以使用lambda表达式进行比较:
stable_sort(boundRect.begin()+5, boundRect.begin()+10,
[](const Rect & a, const Rect &b) { return a.x < b.x; });
stable_排序(boundRect.begin()+5,boundRect.begin()+10,
[](const-Rect&a,const-Rect&b){返回a.x
这不仅更短、更容易阅读,而且通常也会更快(当您只对5个元素进行排序时,这并不重要)。stable\u排序(boundRect.begin()+5,boundRect.begin()+10,compareX\rect);
使用stable\u排序(boundRect.begin()+5,boundRect.begin()+10,compareX\rect);
?使用