一、unique
头文件algorithm
equality (1)
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last);
predicate (2)
template <class ForwardIterator, class BinaryPredicate>
ForwardIterator unique (ForwardIterator first, ForwardIterator last,
BinaryPredicate pred);
删除范围内的连续重复项
从[first,last]范围内的每个连续的等效元素组中删除除第一个元素之外的所有元素。
该函数不能改变包含元素范围的对象的属性(即,它不能改变数组或容器的大小):通过用下一个不重复的元素替换重复元素来完成删除,并且 通过将迭代器返回到应该被视为其新的past-the-end元素的元素来发信号通知缩短范围的新大小。
保留未删除元素的相对顺序,而返回的迭代器和last之间的元素保留在有效但未指定的状态。
该函数使用operator ==来比较元素对(或版本(2)中的pred)。
此函数模板的行为等效于:
template <class ForwardIterator>
ForwardIterator unique (ForwardIterator first, ForwardIterator last)
{
if (first==last) return last;
ForwardIterator result = first;
while (++first != last)
{
if (!(*result == *first)) // or: if (!pred(*result,*first)) for version (2)
*(++result)=*first;
}
return ++result;
}
参数
- first,last
将迭代器转发到move-assignable元素序列的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
二进制函数,接受范围中的两个元素作为参数,并返回可转换为bool的值。 返回的值指示两个参数是否被视为等效(如果为true,则它们是等效的,并且其中一个被删除)。
该函数不得修改其任何参数。这可以是函数指针或函数对象。
返回值
未删除的最后一个元素后面的元素的迭代器。first和this迭代器之间的范围包括序列中不被视为重复的所有元素。
// unique algorithm example
#include <iostream> // std::cout
#include <algorithm> // std::unique, std::distance
#include <vector> // std::vector
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10
std::vector<int> myvector (myints,myints+9);
// using default comparison:
std::vector<int>::iterator it;
it = std::unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ?
// ^
myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30 20 10
// using predicate comparison:
std::unique (myvector.begin(), myvector.end(), myfunction); // (no changes)
// print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
对于非空范围,线性小于第一个和最后一个之间的距离:比较每对连续元素,并可能对其中一些元素执行赋值。
数据范围
访问并可能修改[first,last]范围内的对象。
异常
如果任何pred,元素比较,元素赋值或迭代器上的操作抛出,则抛出。请注意,无效参数会导致未定义的行为。
二、unique_copy
头文件algorithm
equality (1)
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result);
predicate (2)
template <class InputIterator, class OutputIterator, class BinaryPredicate>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result, BinaryPredicate pred);
复制范围删除重复项
将[first,last]范围内的元素复制到从result开始的范围,但连续重复项(比较前面的元素的元素除外)。
仅复制[first,last]范围内每个连续的等效元素组中的第一个元素。
元素之间的比较是通过在它们之间应用operator ==或模板参数pred(对于第二个版本)来执行的。
此函数模板的行为等效于:
template <class InputIterator, class OutputIterator>
OutputIterator unique_copy (InputIterator first, InputIterator last,
OutputIterator result)
{
if (first==last) return result;
*result = *first;
while (++first != last) {
typename iterator_traits<InputIterator>::value_type val = *first;
if (!(*result == val)) // or: if (!pred(*result,val)) for version (2)
*(++result)=val;
}
return ++result;
}
参数
- first,last
将迭代器转发到序列中的初始位置和最终位置。使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。
如果InputIterator是单通道迭代器,它指向的类型应该是可复制构造和可复制分配的。 - result
将迭代器输出到存储结果值范围的范围的初始位置。
指向的类型应支持赋值[first,last]范围内的元素值。 - pred
二进制函数,接受范围中的两个元素作为参数,并返回可转换为bool的值。返回的值指示两个参数是否被视为等效(如果为true,则它们是等效的,并且其中一个被删除)。
该函数不得修改其任何参数。这可以是函数指针或函数对象。
范围不得重叠。
返回值
指向复制范围末尾的迭代器,它不包含连续的重复项。
// unique_copy example
#include <iostream> // std::cout
#include <algorithm> // std::unique_copy, std::sort, std::distance
#include <vector> // std::vector
bool myfunction (int i, int j) {
return (i==j);
}
int main () {
int myints[] = {10,20,20,20,30,30,20,20,10};
std::vector<int> myvector (9); // 0 0 0 0 0 0 0 0 0
// using default comparison:
std::vector<int>::iterator it;
it=std::unique_copy (myints,myints+9,myvector.begin()); // 10 20 30 20 10 0 0 0 0
// ^
std::sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0
// ^
// using predicate comparison:
it=std::unique_copy (myvector.begin(), it, myvector.begin(), myfunction);
// 10 20 30 20 30 0 0 0 0
// ^
myvector.resize( std::distance(myvector.begin(),it) ); // 10 20 30
// print out content:
std::cout << "myvector contains:";
for (it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离最多为线性:比较每对元素,并对那些不匹配的元素执行赋值操作。
数据范围
访问[first,last]范围内的对象。
结果和返回值之间范围内的对象被修改。
异常
如果任何pred,元素比较,元素赋值或迭代器上的操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。
三、remove
头文件algorithm
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
从范围中删除值
[注意:这是算法删除的参考。 请参阅删除的删除。]
将范围[first,last]转换为一个范围,其中所有比较等于val的元素被移除,并将迭代器返回到该范围的新结尾。
该函数不能改变包含元素范围的对象的属性(即,它不能改变数组或容器的大小):删除是通过将下一个不匹配的元素替换为val的元素来完成的。 通过将迭代器返回到应该被视为新的past-the-end元素的元素来发出缩短范围的新大小的信号。
保留未删除元素的相对顺序,而返回的迭代器和last之间的元素保留在有效但未指定的状态。
该函数使用operator ==将各个元素与val进行比较。
元素由move替换 - 为它们分配新值。
此函数模板的行为等效于:
【C++11】
template <class ForwardIterator, class T>
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val)
{
ForwardIterator result = first;
while (first!=last) {
if (!(*first == val)) {
*result = move(*first);
++result;
}
++first;
}
return result;
}
参数
- first,last
将迭代器转发到一系列move-assignable元素中的初始和最终位置,支持与T类型的值进行比较。使用的范围是[first,last],它包含first和last之间的所有元素,包括指向的元素 首先但不是最后指向的元素。 - val
要删除的值。
返回值
未删除的最后一个元素后面的元素的迭代器。
first和this迭代器之间的范围包括序列中不比较等于val的所有元素。
// remove example
#include <iostream> // std::cout
#include <algorithm> // std::remove
int main () {
int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = std::remove (pbegin, pend, 20); // 10 30 30 10 10 ? ? ?
// ^ ^
std::cout << "range contains:";
for (int* p=pbegin; p!=pend; ++p)
std::cout << ' ' << *p;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:比较每个元素,并可能对其中一些元素执行赋值。
数据范围
访问并可能修改[first,last]范围内的对象。
异常
如果任何元素比较,元素赋值或迭代器上的操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。
四、remove_if
头文件algorithm
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred);
从范围中删除元素
将范围[first,last]转换为一个范围,其中包含pred返回true的所有元素,并将迭代器返回到该范围的新结尾。
该函数不能改变包含元素范围的对象的属性(即,它不能改变数组或容器的大小):通过用下一个元素替换pred返回true的元素来完成删除。 没有,并通过将迭代器返回到应该被视为其新的过去元素的元素来发出缩短范围的新大小。
保留未删除元素的相对顺序,而返回的迭代器和last之间的元素保留在有效但未指定的状态。
元素被move替换 - 为它们分配新值。
此函数模板的行为等效于:
template <class ForwardIterator, class UnaryPredicate>
ForwardIterator remove_if (ForwardIterator first, ForwardIterator last,
UnaryPredicate pred)
{
ForwardIterator result = first;
while (first!=last) {
if (!pred(*first)) {
*result = std::move(*first);
++result;
}
++first;
}
return result;
}
参数
- first,last
将迭代器转发到一系列可移动赋值元素中的初始和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - pred
一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示是否要删除元素(如果为true,则将其删除)。
该函数不得修改其参数。这可以是函数指针或函数对象。
返回值
未删除的最后一个元素后面的元素的迭代器。
first和this迭代器之间的范围包括序列中pred不返回true的所有元素。
// remove_if example
#include <iostream> // std::cout
#include <algorithm> // std::remove_if
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9}; // 1 2 3 4 5 6 7 8 9
// bounds of range:
int* pbegin = myints; // ^
int* pend = myints+sizeof(myints)/sizeof(int); // ^ ^
pend = std::remove_if (pbegin, pend, IsOdd); // 2 4 6 8 ? ? ? ? ?
// ^ ^
std::cout << "the range contains:";
for (int* p=pbegin; p!=pend; ++p)
std::cout << ' ' << *p;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:对每个元素应用pred,并可能对其中一些元素执行赋值。
数据范围
访问并可能修改[first,last]范围内的对象。
异常
如果任何pred,元素赋值或迭代器上的操作抛出则抛出。
请注意,无效参数会导致未定义的行为。
五、remove_copy
头文件algorithm
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& val);
复制范围删除值
将[first,last]范围内的元素复制到从result开始的范围,除了那些比较等于val的元素。
得到的范围比[第一个,最后一个]短了与序列中的匹配一样多的元素,这些元素被“移除”。
该函数使用operator ==将各个元素与val进行比较。
此函数模板的行为等效于:
template <class InputIterator, class OutputIterator, class T>
OutputIterator remove_copy (InputIterator first, InputIterator last,
OutputIterator result, const T& val)
{
while (first!=last) {
if (!(*first == val)) {
*result = *first;
++result;
}
++first;
}
return result;
}
参数
- first,last
将迭代器转发到元素序列中的初始位置和最终位置,支持与类型T的值进行比较。使用的范围是[first,last],其中包含first和last之间的所有元素,包括第一个指向的元素但是 不是最后指向的元素。 - result
将迭代器输出到存储结果序列的范围的初始位置。
指向的类型应支持赋值[first,last]范围内的元素值。 - val
要删除的值。
范围不得重叠。
返回值
指向复制范围末尾的迭代器,其中包括[first,last]中的所有元素,除了那些比较等于val的元素。
// remove_copy example
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy
#include <vector> // std::vector
int main () {
int myints[] = {10,20,30,30,20,10,10,20}; // 10 20 30 30 20 10 10 20
std::vector<int> myvector (8);
std::remove_copy (myints,myints+8,myvector.begin(),20); // 10 30 30 10 10 0 0 0
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:比较每个元素,并对未删除的元素执行赋值操作。
数据范围
访问[first,last]范围内的对象。
结果和返回值之间范围内的对象被修改。
异常
如果任何元素比较,元素赋值或迭代器上的操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。
六、remove_copy_if
头文件algorithm
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator remove_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred);
复制范围删除值
将[first,last]范围内的元素复制到从result开始的范围,但pred返回true的元素除外。
得到的范围比[第一个,最后一个]短的匹配元素数量被“移除”。
此函数模板的行为等效于:
template <class InputIterator, class OutputIterator, class UnaryPredicate>
OutputIterator remove_copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred)
{
while (first!=last) {
if (!pred(*first)) {
*result = *first;
++result;
}
++first;
}
return result;
}
参数
- first,last
将迭代器转发到序列中的初始位置和最终位置。 使用的范围是[first,last),它包含first和last之间的所有元素,包括first指向的元素,但不包括last指向的元素。 - result
将迭代器输出到存储结果序列的范围的初始位置。
指向的类型应支持赋值[first,last]范围内的元素值。 - pred
一元函数接受范围内的元素作为参数,并返回可转换为bool的值。 返回的值指示是否要从副本中删除元素(如果为true,则不会复制该元素)。
该函数不得修改其参数。这可以是函数指针或函数对象。
范围不得重叠。
返回值
指向复制范围末尾的迭代器,其中包括[first,last]中的所有元素,但pred返回true的元素除外。
// remove_copy_if example
#include <iostream> // std::cout
#include <algorithm> // std::remove_copy_if
#include <vector> // std::vector
bool IsOdd (int i) { return ((i%2)==1); }
int main () {
int myints[] = {1,2,3,4,5,6,7,8,9};
std::vector<int> myvector (9);
std::remove_copy_if (myints,myints+9,myvector.begin(),IsOdd);
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
复杂度
第一个和最后一个之间的距离线性:对每个元素应用pred,并对未删除的元素执行赋值操作。
数据范围
访问[first,last]范围内的对象。
结果和返回值之间范围内的对象被修改。
异常
如果任何pred,元素赋值或迭代器上的操作抛出,则抛出。
请注意,无效参数会导致未定义的行为。