accumulate用于容器元素的“累運算”,累加,累乘,等等,默認累加。
accumulate (coll.begin(), coll.end()) ;
accumulate (coll.begin(), coll.end(), // range
1, // initial value
multiplies<int>()) // operation
adjacent_difference創(chuàng )建一個(gè)新序列,用于相鄰元素的運算,默認為差值。
adjacent_difference (coll.begin(), coll.end(), // source
ostream_iterator<int>(cout," ")); // dest.
adjacent_difference (coll.begin(), coll.end(), // source
iresult.begin(), // dest.
plus<int>()); // operation
adjacent_find 用于容器元素相鄰元素的查找,默認為相等。
pos = adjacent_find (coll.begin(), coll.end());
bool doubled (int elem1, int elem2)
{
return elem1 * 2 == elem2;
}
pos = adjacent_find (coll.begin(), coll.end(), // range
doubled); // criterion
一些常用的運算函數對象
plus<int>() 加
multiplies<int>() 乘
times<int>() 乘
greater<int>() 大于
modulus<int>() 取模
less<int>() 小于
negate<int>() 符號取反
binary_search查找某個(gè)元素是否存在,返回布爾值。
binary_search(coll.begin(), coll.end(), 5)
copy( InputIterator first1, InputIterator last, OutputIterator first2 );
copy()把由[first,last)標記的序列中的元素 拷貝到由 first2 標記為開(kāi)始的地方
copy (coll1.begin(), coll1.end(), // source range
back_inserter(coll2)); // destination range,這種方式一般是coll2只是被聲明,還沒(méi)有元素。
copy (coll2.begin(), coll2.end(), // source range
ostream_iterator<int>(cout," ")); // destination range
copy (coll1.rbegin(), coll1.rend(), // source range
coll2.begin()); // destination range,這種方式必須保證coll2已經(jīng)分配了足夠的空間。
copy (istream_iterator<string>(cin), // beginning of source
istream_iterator<string>(), // end of source 缺省構造函數,表示結尾。
ostream_iterator<string>(cout,"\n")); // destination 從cin讀入,按行輸出到cout
count查找數量,count_if滿(mǎn)足某個(gè)條件函數的元素數量
num = count (coll.begin(), coll.end(), // range
4);
bool isEven (int elem)
{
return elem % 2 == 0;
}
num = count_if (coll.begin(), coll.end(), // range
isEven);
fill()將 value 的拷貝賦給[first,last)范圍內的所有元素
f i l l _ n ( )把 value 的拷貝賦給[first,first+count)范圍內的 count 個(gè)元素
// insert "hello" nine times
fill_n(back_inserter(coll), // beginning of destination
9, // count
"hello"); // new value
// overwrite all elements with "again"
fill(coll.begin(), coll.end(), // destination
"again"); // new value
find()、find_if()
// find first element with value 4
list<int>::iterator pos1;
pos1 = find (coll.begin(), coll.end(), // range
4); // value
// find first element greater than 3
pos = find_if (coll.begin(), coll.end(), // range
bind2nd(greater<int>(),3)); // criterion
find_end()
在由[first,last)標記的序列中查找 由 iterator 對[first2,last2)標記的第二個(gè)序列的最后一次出現.
// search last occurrence of subcoll in coll
deque<int>::iterator pos;
pos = find_end (coll.begin(), coll.end(), // range
subcoll.begin(), subcoll.end()); // subrange
find_first_of()
由[first2,last2)標記的序列包含了一組元素的集合 find_first_of()將在由[first1,last1)標記的序列中搜索這些元素。注意與find_end不同的一點(diǎn)是這里是搜元素,而find_end是搜整個(gè)匹配。
INSERT_ELEMENTS(coll,1,11);
INSERT_ELEMENTS(searchcoll,3,5);
// search first occurrence of an element of searchcoll in coll
vector<int>::iterator pos;
pos = find_first_of (coll.begin(), coll.end(), // range
searchcoll.begin(), // beginning of search set
searchcoll.end()); // end of search set
// search last occurrence of an element of searchcoll in coll
vector<int>::reverse_iterator rpos; // 用了反向iterator
rpos = find_first_of (coll.rbegin(), coll.rend(), // range
searchcoll.begin(), // beginning of search set
searchcoll.end()); // end of search set
for_each()依次對[first,last)范圍內的所有元素應用函數 func func 不能對元素執行寫(xiě)操作
void print (int elem)
{
cout << elem << ' ';
}
// call print() for each element
for_each (coll.begin(), coll.end(), // range
print); // operation 函數指針
#include "algostuff.hpp"
using namespace std;
// function object that adds the value with which it is initialized
template <class T>
class AddValue {
private:
T theValue; // value to add
public:
// constructor initializes the value to add
AddValue (const T& v) : theValue(v) {
}
// the function call for the element adds the value
void operator() (T& elem) const {
elem += theValue;
}
};
函數對象必須實(shí)現()操作符。
int main()
{
vector<int> coll;
INSERT_ELEMENTS(coll,1,9);
// add ten to each element
for_each (coll.begin(), coll.end(), // range
AddValue<int>(10)); // operation 函數對象實(shí)例
PRINT_ELEMENTS(coll);
// add value of first element to each element
for_each (coll.begin(), coll.end(), // range
AddValue<int>(*coll.begin())); // operation
PRINT_ELEMENTS(coll);
}
generate( ForwardIterator first,
ForwardIterator last, Generator gen );
g e n e r a t e ( )通過(guò)對 gen的連續調用 來(lái)填充一個(gè)序列的[first,last)范圍 gen可以是函數對象或函數指針
generate_n( OutputIterator first, Size n, Generator gen );
generate_n()通過(guò)對 gen的 n 次連續調用 來(lái)填充一個(gè)序列中從 first 開(kāi)始的n個(gè)元素 gen可以是函數對象或函數指針
list<int> coll;
// insert five random numbers
generate_n (back_inserter(coll), // beginning of destination range
5, // count
rand); // new value generator
PRINT_ELEMENTS(coll);
// overwrite with five new random numbers
generate (coll.begin(), coll.end(), // destination range
rand); // new value generator
includes( )判斷[first1,last1)的每一個(gè)元素是否被包含在序列[first2,last2)中
void
iter_swap ( ForwardIterator1 a, ForwardIterator2 b );
i t e r _ s w a p ( )交換由兩個(gè) ForwardIterator a 和 b 所指向的元素中的值
template< class Type >
void
swap ( Type &ob1, Type &ob2 );
s w a p ( )交換存貯在對象 ob1 和 ob2 中的值
swap( vec[iy], vec[ix] );
swap_range()將[first,last)標記的元素值與 從 first2 開(kāi)始 相同個(gè)數 的元素值進(jìn)行交換,這兩個(gè)序列可以是同一容器中不相連的序列,也可以位于兩個(gè)獨立的容器中。
pos = swap_ranges (coll1.begin(), coll1.end(), // first range
coll2.begin()); // second range
m a x ( )返回 aval 和 bval 兩個(gè)元素中較大的一個(gè)。
max_element()返回一個(gè)iterator 指向[first,last)序列中值為最大的元素
m i n ( )返回 aval 和 bval 兩個(gè)元素中較小的一個(gè)。
min_element()返回一個(gè) iterator 指向[first,last)序列中值為最小的元素
r e m o v e ( )刪除在[first,last)范圍內的所有value 實(shí)例, remove()以及 remove_if()并不真正地 把匹配到的元素從容器中清除 (即容器的大小保留不變), 而是每個(gè)不匹配的元素依次被賦值給從 first 開(kāi)始的下一個(gè)空閑位置上,返問(wèn)的ForwardIterator 標記了新的元素范圍的下一個(gè)位置。例如,考慮序列{0,1,0,2,0,3,0,4} 假設我們希望刪除所有的 0 ,則結果序列是 {1,2,3,4,0,3,0,4}。 1 被拷貝到第一個(gè)位置上, 2 被拷貝到第二個(gè)位置上, 3被拷貝到第三個(gè)位置上,4 被拷貝到第四個(gè)位置上,返回的ForwardIterator 指向第五個(gè)位置上的 0。典型的做法是該 iterator 接著(zhù)被傳遞給 erase() ,以便刪除無(wú)效的元素 內置數組不適合于使用 remove() 和 remove_if()算法,因為它們不能很容易地被改變大小,由于這個(gè)原因 對于數組而言 remove_copy()和 remove_copy_if()是更受歡迎的算法。
// remove all elements with value 5
pos = remove(coll.begin(), coll.end(), // range
5); // value to remove
// erase the ``removed'' elements in the container
coll.erase(pos, coll.end());
// remove all elements less than 4
coll.erase(remove_if(coll.begin(), coll.end(), // range
bind2nd(less<int>(),4)), // remove criterion
coll.end());
remove_copy()把所有不匹配的元素都拷貝到由 result 指定的容器中,返回的OutputIterator 指向被拷貝的末元素的下一個(gè)位置,但原始容器沒(méi)有被改變。
// print elements without those having the value 3
remove_copy(coll1.begin(), coll1.end(), // source
ostream_iterator<int>(cout," "), // destination
3); // removed value
replace()將[first,last)范圍內的所有 old_value 實(shí)例都用 new_value 替代
replace_copy()的行為與 replace()類(lèi)似 只不過(guò)是把新序列拷貝到由result 開(kāi)始的容器內
replace_if()、replace_copy_if()
// replace all elements with value 6 with 42
replace (coll.begin(), coll.end(), // range
6, // old value
42); // new value
// replace all elements with value less than 5 with 0
replace_if (coll.begin(), coll.end(), // range
bind2nd(less<int>(),5), // criterion for replacement
0); // new value
// print all elements with value 5 replaced with 55
replace_copy(coll.begin(), coll.end(), // source
ostream_iterator<int>(cout," "), // destination
5, // old value
55); // new value
r e v e r s e ( )對于容器中[first,last)范圍內的元素重新按反序排列 例如 已知序列{0,1,1,2,3} ,則反序序列是{3,2,1,1,0}
reverse_copy()
// reverse order of elements
reverse (coll.begin(), coll.end());
// print all of them in reverse order
reverse_copy (coll.begin(), coll.end(), // source
ostream_iterator<int>(cout," ")); // destination
r o t a t e ( )把[first,middle)范圍內的元素移到容器末尾,由 middle 指向的元素成為容器的第一個(gè)元素。例如,已知單詞 hissboo 則以元素 b 為軸的旋轉將單詞變成 boohiss
rotate_copy()
rotate (coll.begin(), // beginning of range
coll.begin() + 1, // new first element
coll.end()); // end of range
// rotate so that element with value 4 is the beginning
rotate (coll.begin(), // beginning of range
find(coll.begin(),coll.end(),4), // new first element
coll.end()); // end of range
search()
給出了兩個(gè)范圍 ,search()返回一個(gè)iterator 指向在[first1,last1)范圍內第一次出現子序列[first2,last2]的位置。 如果子序列未出現,則返回 last1。例如,在 mississippi 中 子序列iss 出現兩次,則 search()返回一個(gè)iterator 指向第一個(gè)實(shí)例的起始處。缺省情況下 使用等于操作符進(jìn)行元素的比較,第二個(gè)版本允許用戶(hù)提供一個(gè)比較操作。
pos = search (coll.begin(), coll.end(), // range
subcoll.begin(), subcoll.end()); // subrange
// loop while subcoll found as subrange of coll
while (pos != coll.end())
search_n()在[first,last)序列中查找 value 出現 count 次 的子序列
set_difference()構造一個(gè)排過(guò)序的序列,其中的元素出現在第一個(gè)序列中,由[first,last)標記,但是不包含在第二個(gè)序列中,由[first2,last2]標記。例如,已失兩個(gè)序列{0,1,2,3} 和{0,2,4,6} 則差集為{1,3}
set_intersection()構造一個(gè)排過(guò)序的序列,其中的元素在[first1,last1)和[first2,last2)序列中都存在。例如,已知序列{0,1,2,3}和{0,2,4,6} 則交集為{0,2}。
set_union()構造一個(gè)排過(guò)序的序列,它包含了[first1,last1)和[first2,last2)這兩個(gè)范圍內的所有元素。例如,已知兩個(gè)序列{0,1,2,3}和{0,2,4,6},則并集為{0,1,2,3,4,6}。
s o r t ( )利用底層元素的小于操作符 以升序重新排列[first,last)范圍內的元素。
// sort elements
sort (coll.begin(), coll.end());
// sorted reverse
sort (coll.begin(), coll.end(), // range
greater<int>()); // sorting criterion
bool lessLength (const string& s1, const string& s2)
{
return s1.length() < s2.length();
}
// sort (according to the length of the strings)
sort (coll1.begin(), coll1.end(), // range
lessLength); // criterion
stable_sort()利用底層類(lèi)型的小于操作符 以升序重新排列[first,last)范圍內的元素 并且
保留相等元素之間的順序關(guān)系
t r a n s f o r m ( )的第一個(gè)版本將 op 作用在[first,last)范圍內的每個(gè)元素上,從而產(chǎn)生一個(gè)新的序列。例如,已知序列{0,1,1,2,3,5}和函數對象 Double 它使每個(gè)元素加倍 那么 結果序列是{0,2,2,4,6,10}
// negate all elements in coll1
transform (coll1.begin(), coll1.end(), // source range
coll1.begin(), // destination range
negate<int>()); // operation
// transform elements of coll1 into coll2 with ten times their value
transform (coll1.begin(), coll1.end(), // source range
back_inserter(coll2), // destination range
bind2nd(multiplies<int>(),10)); // operation
第二個(gè)版本將bop 作用在一對元素上,其中一個(gè)元素來(lái)自序列[first1,last), 另一個(gè)來(lái)自由first2 開(kāi)始的序列 最終產(chǎn)生一個(gè)新的序列。如果第二個(gè)序列包含的元素少于第一個(gè)序列,則運行時(shí)刻行為是未定義的。例如,已知序列{1,3,5,9}和{2,4,6,8} 以及函數對象 AddAndDouble,它把兩個(gè)元素相加并將和加倍,則結果序列是{6,14,22,34}
// square each element
transform (coll1.begin(), coll1.end(), // first source range
coll1.begin(), // second source range
coll1.begin(), // destination range
multiplies<int>()); // operation
unique()對于連續的元素, 如果它們包含相同的值(使用底層類(lèi)型的等于操作符來(lái)判斷)或者把它們傳給pred的計算結果都為 true,則這些元素被折疊成一個(gè)元素。實(shí)際上 unique()的行為有些不太直觀(guān),類(lèi)似于 remove()算法。典型的做法是 這個(gè) iterator 被傳遞給 erase() 以便刪除無(wú)效的元素,由于內置數組不支持erase()操作,所以unique()不太適合于數組,unique_copy()對數組更為合適一些。
其實(shí)就是運用泛型算法時(shí),算法不會(huì )去改變原容器的大小,所以remove和unique算法才會(huì )有此現象。
pos = unique (coll.begin(), coll.end());
// print elements
copy (coll.begin(), pos, // source
ostream_iterator<int>(cout," ")); // destination