C++学习笔记26——泛型算法之容器元素排序(sort unique)
来源:互联网 发布:最优化pdf网盘 编辑:程序博客网 时间:2024/05/07 23:34
1,sort函数
template< class RandomIt >void sort( RandomIt first, RandomIt last );template< class RandomIt, class Compare >void sort( RandomIt first, RandomIt last, Compare comp );
Sorts the elements in the range [first, last)
in ascending order.The order of equal elements is not guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function object comp
.
效果:对范围[first, last)之间的元素进行排序,不稳定算法。即排序后,值相等的元素位置位置不固定。
第一个版本,使用操作符“<”去比较,实现升序排列;
第二个版本使用谓词comp去比较,bool comp(const Type1 &a, const Type2 &b);
2,stable_sort函数
template< class RandomIt >void stable_sort( RandomIt first, RandomIt last );template< class RandomIt, class Compare >void stable_sort( RandomIt first, RandomIt last, Compare comp );
Sorts the elements in the range
[first, last)
in ascending order.The order of equal elements is guaranteed to be preserved. The first version uses operator< to compare the elements, the second version uses the given comparison function comp
.3,unique函数
template< class ForwardIt >ForwardIt unique( ForwardIt first, ForwardIt last );template< class ForwardIt, class BinaryPredicate >ForwardIt unique( ForwardIt first, ForwardIt last, BinaryPredicate p );
Removes all consecutive duplicate elements from the range [first, last)
and returns a past-the-end iterator for the new logical end of the range. The first version uses operator==
to compare the elements, the second version uses the given binary predicate p
.
Removing is done by shifting the elements in the range in such a way that elements to be erased are overwritten. Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values. A call to unique
is typically followed by a call to a container'serase
method, which erases the unspecified values and reduces the physical size of the container to match its newlogical size.
/*******************************************************************/// 验证unique/*******************************************************************/string names("I Loveee U! Do U Know?");unique(names.begin(), names.end());cout << names << endl;输出为:
/*******************************************************************/// 验证unique/*******************************************************************/string names("I Loveee U! Do U Know?");sort(names.begin(), names.end());cout << "before sort: " << names << endl;string::iterator logic_end = unique(names.begin(), names.end());cout << "before erase: " << names << endl;names.erase(logic_end,names.end());cout << "after erase: " << names << endl;输出为:
4,unique_copy函数
template< class InputIt, class OutputIt >OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first );template< class InputIt, class OutputIt, class BinaryPredicate >OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first, BinaryPredicate p );
为unique的_copy版本,将输入范围中不重复的值复制到目标迭代器。
需要注意的是:d_first中复制的仅仅是不重复的部分。
template< class InputIt, class OutputIt >OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first );template< class InputIt, class OutputIt, class BinaryPredicate >OutputIt unique_copy( InputIt first, InputIt last, OutputIt d_first, BinaryPredicate p );
- C++学习笔记26——泛型算法之容器元素排序(sort unique)
- 泛型算法——容器元素的排序算法(统计长度不小于6的单词个数)sort unique stable_sort count_if
- C++语法基础--泛型算法(generic algorithm)--对容器排序的算法sort(),stable_sort(),unique()
- 泛型算法之sort,unique,stable_sort
- C++泛型算法之 sort() stable_sort() unique() count_if()
- C++学习笔记25——泛型算法之写入容器
- C语言练习0之数组——数组元素求和与数组冒泡排序(bubble sort)
- STL学习笔记之sort算法
- c++primer学习笔记 - 容器和算法
- 数据结构(c语言版)学习笔记——排序算法
- 数据结构(c语言版)学习笔记——排序算法
- STL源码剖析——list容器的排序算法sort()
- STL源码剖析——list容器的排序算法sort()
- 【C++ Primer 学习笔记】: 容器和算法之【泛型算法】
- C++ primer 十九 泛型算法replace/replace_copy/sort/unique
- 算法分析之——quick-sort快速排序
- 算法分析之——heap-sort堆排序
- C++ 学习笔记 2.1 : 容器和算法(顺序容器、关联容器、泛型算法)
- android 自定义ViewGroup实现仿淘宝的商品详情页
- spring+jpa 两个数据源配置
- Map集合中的两种取出方式keySet和entrySet
- 可视化河流图基础要点
- Activity的静态start方法
- C++学习笔记26——泛型算法之容器元素排序(sort unique)
- Paint及Canvas的简单应用
- 文章标题
- OC的封装继承和多态
- mysql中int、bigint、smallint 和 tinyint的区别详细介绍
- 欢迎使用CSDN-markdown编辑器
- UIWebView清除缓存和cookie
- HashMap Vs HashTable
- Tomcat之——接收UTF-8编码GET参数请求配置