网站重定向过多,企业企业网站建设,小白怎样建设公司网站,哪家网站做旅游攻略好算法(2)----STL里的排序函数。
1. sort: 对容器或普通数组中指定范围内的元素进行排序#xff0c;默认进行升序排序。 sort函数是基于快速排序实现的#xff0c;属于不稳定排序。 只支持3种容器#xff1a;array、vector、deque。 如果容器中存储的是自定义的对象#xff…算法(2)----STL里的排序函数。
1. sort: 对容器或普通数组中指定范围内的元素进行排序默认进行升序排序。 sort函数是基于快速排序实现的属于不稳定排序。 只支持3种容器array、vector、deque。 如果容器中存储的是自定义的对象则该类必须提供移动构造函数和移动赋值运算符。 代码示例
class AA
{int* m_pValue;
public:AA(int v) :m_pValue(new int(v)) {}//拷贝构造函数AA(const AA other) {if (0 ! other.m_pValue) {this-m_pValue new int(*other.m_pValue);}else {this-m_pValue 0;}}//析构函数~AA() {delete m_pValue;}//移动构造函数AA(AA other) noexcept : m_pValue(other.m_pValue) {other.m_pValue 0;}//移动赋值操作符AA operator (AA other) noexcept {if (this ! other) {delete m_pValue;m_pValue other.m_pValue;other.m_pValue 0;}return *this;}//比较操作符bool operator (AA other) {if (0 ! this-m_pValue 0 ! other.m_pValue) {return *this-m_pValue *other.m_pValue;}return false;}void Print() {if (0 ! this-m_pValue) {cout *this-m_pValue ;}else {cout null ;}}
};int main() {std::vectorAA v1{ 5,6,9,8,3,2,1,4 };for_each(v1.begin(), v1.end(), mem_fun_ref(AA::Print));cout endl;sort(v1.begin(), v1.end());for_each(v1.begin(), v1.end(), mem_fun_ref(AA::Print));return 0;
}
2. stable_sort: 排序后保证相等元素的相对位置和排序前是一样的。 stable_sort函数是基于归并排序实现的属于稳定排序。用法和sort一样。
3. partial_sort(first, middle, last) 从指定范围内选出(middle-first)个最小的元素并排序存放在 [first,middle) 区间。 代码示例
void printInt(int val)
{cout val ;
}
int main() {std::vectorint v1{ 3,2,5,4,1,6,9,8 };for_each(v1.begin(), v1.end(), printInt);cout endl;//将v1中最小的 3 个元素移动到开头位置并排好序partial_sort(v1.begin(), v1.begin() 3, v1.end());for_each(v1.begin(), v1.end(), printInt);return 0;
}
4. partial_sort_copy(first, last, result_first, result_last) 从指定范围内选出(result_last-result_first)个元素排序后拷贝到另一个容器。 代码示例
void printInt(int val)
{cout val ;
}
int main() {int target[4] { 0 };std::vectorint v1{ 3,2,5,4,1,6,9,8 };//将v1中前面5个元素排序然后拷贝3个元素到targetpartial_sort_copy(v1.begin(), v1.begin() 5, target, target 3);for_each(target, target 4, printInt);return 0;
}
5. nth_element (first, nth, last) 找到[first, last)范围内按照排序规则(默认升序)位于第nth个位置处的元素并将其放置到此位 置。同时使所有比此元素小的元素在左侧比它大的元素在右侧。
void printInt(int val)
{cout val ;
}
int main() {std::vectorint v1{ 8,1,3,4,5,6,0,2,7,9 };//默认升序排序nth_element(v1.begin(), v1.begin() 2, v1.end());cout nth_element排序 endl;for_each(v1.begin(), v1.end(), printInt);return 0;
}
6. partition (first, last, pred) 根据用户自定义的筛选规则重新排列指定区域内存储的数据使其分为 2 组第一组为符合 筛选条件的数据另一组为不符合筛选条件的数据。返回第二组的第一个元素。 代码示例
void printInt(int val)
{cout val ;
}
bool compare(int i) { return (i % 2) 0; }
int main() {std::vectorint v1{ 1,2,3,4,5,6,7,8,9 };auto bound partition(v1.begin(), v1.end(), compare);//按奇偶分组cout bound *boundendl;for_each(v1.begin(), v1.end(), printInt);return 0;
}
7. stable_partition (first, last, pred) 保证对指定区域内数据完成分组的同时不改变各组内元素的相对位置。用法和partition一样。
8. is_sorted (first, last, comp) 此函数专门用于判断某个序列是否为有序序列。 代码示例
bool compare(int i, int j) { return i j; }
int main() {std::vectorint v1{ 9, 8, 7, 6, 2 };cout v1 is sorted? is_sorted(v1.begin(), v1.end(), compare) endl;return 0;
}