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 );