(1)adjacent_find
找出第一组满足条件的相邻元素。这里所谓的条件,在版本一中是指“两元素相等”,在版本二中允许用户指定一个二元运算,两个操作数分别是相邻的第一个元素和第二个元素。
-
//查找相邻的重复元素,版本一
-
template <class ForwardInterator>
-
ForwardInterator adjacent_find(ForwardInterator first, ForwardInterator last)
-
{
-
if(first == last)
-
{
-
return last;
-
}
-
ForwardInterator next = first;
-
while(++next != last)
-
{
-
if(*next == *first)
-
{
-
return first; //如果找到相邻的元素值相等,就结束
-
}
-
first = next;
-
}
-
return last;
-
}
-
-
//查找相邻重复元素,版本二
-
template <class ForwardInterator, class BinaryPredicate>
-
ForwardInterator adjacent_find(ForwardInterator first, ForwardInterator last, BinaryPredicate binary_pred)
-
{
-
if(first == last)
-
{
-
return last;
-
}
-
ForwardInterator next = first;
-
while(++next != last)
-
{
-
//如果找到相邻的元素符合外界指定的条件,就结束
-
if(binary_pred(*first, *next))
-
{
-
return first; //如果找到相邻的元素值相等,就结束
-
}
-
first = next;
-
}
-
return last;
- }
(2)count
运用equaltiy操作符,将[first, last)区间内的每一个元素拿来与指定值value比较,并返回与value相等的元素个数。
-
template < class InputInterator, class T>
-
typename intertor_traits<InputInterator>::difference_type
-
count(InputInterator first, InputInterator last, const T&value)
-
{
-
//以下声明一个计数器n
-
typename iterator_traits<InputInterator>::difference_type n = 0;
-
for( ; first != last; first++)
-
{
-
if(*first == value)
-
{
-
n++;
-
}
-
}
-
return n;
- }
(3)count_if
将指定操作(一个仿函数)pred实施于[first, last)区间内的每一个元素身上,并将“造成pred之计算结果为true”的所有元素的个数返回。
-
template < class InputInterator, class BinaryPredice>
-
typename intertor_traits<InputInterator>::difference_type
-
count_if(InputInterator first, InputInterator last, BinaryPredice binary_pred)
-
{
-
//以下声明一个计数器n
-
typename iterator_traits<InputInterator>::difference_type n = 0;
-
for( ; first != last; first++)
-
{
-
if(binary_pred(*first))
-
{
-
n++;
-
}
-
}
-
return n;
- }
(4)find
根据equality操作符,循序查找[first, last)内的所有元素,找出第一个匹配“等同条件”者。如果找到,就返回一个InputIterator指向该元素,否则返回迭代器last。
-
template <class InputInterator, class T>
-
InputInterator find(InputInterator first, InputInterator last, const T &value)
-
{
-
while(first != last && *first != value)
-
{
-
first++;
-
}
-
return first;
- }
(5)find_if
根据指定的pred运算条件(以仿函数表示),循序查找[first, last)内的所有元素,找出第一个令pred运算结果为true者。
-
template <class InputInterator, class BinaryPredice>
-
InputInterator find(InputInterator first, InputInterator last, BinaryPredice pred)
-
{
-
while(first != last && !pred(*first))
-
{
-
first++;
-
}
-
return first;
- }
(6)find_end
在序列一[first1, last1)所涵盖的区间中,查找序列二[first2, last2)的最后一次出现点。如果序列一之内不存在“完全匹配序列二”的子序列,便返回迭代器last1,。此算法有两个版本,版本一使用元素型别所提供的equality操作符,版本二允许用户指定某个二元运算(以仿函数呈现),作为判断元素相等与否的依据。
(7)find_first_of
该算法以[first2, last2)区间内的某些元素作为查找目标,寻找它们在[first1, last1)区间内的第一个出现的地点。
(8)for_each
将仿函数f施行于[first, last)区间内的每一个元素身上。f不可以改变元素内容,因为first和last都是InputIterators,不保证接受赋值行为。
-
template <class InputIterator, class Function>
-
Function for_each(InputIterator first, InputIterator last, Function f)
-
{
-
for( ; first != last; first++)
-
{
-
f(*first); //调用仿函数f的function call操作符。返回值被忽略
-
}
-
return f;
- }
(9)generate
将仿函数gen的运算结果填写在[first, last)区间内的所有元素身上。所谓填写,用的是迭代器所指元素之assignment操作符。
(10)generate_n
将仿函数gen的运算结果填写在从迭代器first开始的n个元素身上。所谓填写,用的是迭代器所指元素之assignment操作符。
(11)includes(应用于有序区间)
判断序列二s2是否“涵盖于”序列一s1。s1和s2都必须是有序集合,其中的元素都可以重复,不必唯一。所谓涵盖,意思是“s2中的每一个元素都出现于s1中”。
includes算法可供用户选择采用less或greater进行两元素的大小比较。
换句话说,如果s1和s2是递增排序,includes算法应该这样使用:
- includes<s1.begin(), s1.end(), s2.begin(), s2.end());
- includes<s1.begin(), s1.end(), s2.begin(), s2.end(), less<int>());
- includes<s1.begin(), s1.end(), s2.begin(), s2.end(),greater<int>());
(12)max_element
这个算法返回一个迭代器,指向序列之中数值最大的元素。
(13)merge(应用于有序区间)
将两个经过排序的集合s1和s2,合并起来置于另一段空间。所得结果也是一个有序序列。返回一个迭代器,指向最后结果序列的最后一个元素的下一位置。
版本一的代码。
-
template <class InputIterator1, class InputIterator2, class OutputIterator>
-
OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result)
-
{
-
while(first1 != last1 && first2 != last2)
-
{
-
if(*first1 < *first2)
-
{
-
*result = first1;
-
first1++;
-
}
-
else
-
{
-
*result = *first2;
-
first2++;
-
}
-
result++;
-
}
-
-
//最后剩余元素以copy复制到目的端。以下两个序列一定至少有一个为空
-
copy(first2, last2, copy(first1, last1, result));
- }
(14)min_element
这个算法返回一个迭代器,指向序列之中数值最小的元素。
(15)partition
partition会将区间[first, last)中的元素重新排列。所有被一元条件运算pred判定为true的元素,都会被放到区间的前段,被判定为false的元素,都会被放到区间的后段。这个算法并不保留元素的原始相对位置。如果需要保留原始相对位置,应使用stable_partition。