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.

效果:稳定的sort排序。
          与sort类似,区别就是,具有稳定性。排序前后相等元素的相对位置关系保持不变。

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.


效果:“删除”所有相邻的重复元素。被删除的元素被移到了容器的末端。因为算法不能改变容器的大小。
     第一个版本用"=="操作符来判断元素是否相等;
     第二个版本通过谓词p来判断,p的原型为bool pred(const Type1 &a, const Type2 &b);

这两个函数的剔除字符原理是,看当前字符与他前一个字符是否相同,如果相同就剔除当前字符,如果不同就跳转到下一个字符。所以在求一个字符串的字符集的时候要先把字符串排个序再调用上面两个函数剔除重复字符,获取字符集。

注意:(1)该函数只删除相邻的相同元素
     (2)被移到末尾的被删除元素的值其实是不确定的
如下例所示:
/*******************************************************************///          验证unique/*******************************************************************/string names("I Loveee U! Do U Know?");unique(names.begin(), names.end());cout << names << endl;
输出为:


可以看到两点:不相邻的重复单词没有被删除,比如“U”;
           末尾的单词被修改了。

所以通常的用法是:
(1)在调用unique之前调用sort排序,这样相同的元素必然相邻;
(2)在调用unique之后调用容器自身的erase方法来删除末端的元素。

如下所示:
/*******************************************************************///          验证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中复制的仅仅是不重复的部分。
0 0
原创粉丝点击