?
?
第22章 變易算法? Modifying? sequence operations
?? 22.1 元素復(fù)制copy copy? Copy range of elements (function template)??? ?? 22.2 反向復(fù)制copy_backward copy_backward? Copy range of elements backwards (function template)??? ?? 22.3 元素交換swap swap? Exchange values of two objects (function template)??? ?? 22.4 迭代器交換iter_swap iter_swap? Exchange values of objects pointed by two iterators (function template)??? ?? 22.5 區(qū)間元素交換swap_ranges swap_ranges? Exchange values of two ranges (function template)??? ?? 22.6 元素變換transform transform? Apply function to range (function template)??? ?? 22.7 替換Replace replace? Replace value in range (function template)??? ?? 22.8 條件替換replace_if replace_if? Replace values in range (function template)??? ?? 22.9 替換和復(fù)制replace_copy replace_copy? Copy range replacing value (function template)??? ?? 22.10 條件替換和復(fù)制replace_copy_if replace_copy_if? Copy range replacing value (function template)??? ?? 22.11 填充fill fill? Fill range with value (function template)??? ?? 22.12 n次填充fill_n fill_n? Fill sequence with value (function template)??? ?? 22.13 隨機生成元素generate generate? Generate values for range with function (function template)??? ?? 22.14 隨機生成n個元素generate_n generate_n? Generate values for sequence with function (function template)??? ?? 22.15 移除復(fù)制remove_copy remove_copy? Copy range removing value (function template) ?? 22.16 條件移除復(fù)制remove_copy_if remove_copy_if? Copy range removing values (function template) ?? 22.17 移除remove remove? Remove value from range (function template) ?? 22.18 條件移除remove_if remove_if? Remove elements from range (function template) ?? 22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy unique_copy? Copy range removing duplicates (function template) ?? 22.20 剔除連續(xù)重復(fù)元素unique unique? Remove consecutive duplicates in range (function template) ?? 22.21 元素反向reverse reverse? Reverse range (function template) ?? 22.22 反向復(fù)制reverse_copy reverse_copy? Copy range reversed (function template) ?? 22.23 旋轉(zhuǎn)rotate rotate? Rotate elements in range (function template) ?? 22.24 旋轉(zhuǎn)復(fù)制rotate_copy rotate_copy? Copy rotated range (function template) ?? 22.25 隨機抖動random_shuffle random_shuffle? Rearrangle elements in range randomly (function template)??? ?? 22.26 隨機采樣random_sample ... ?? 22.27 容器分割partition partition? Partition range in two (function template)??? ?? 22.28 容器穩(wěn)定分割stable_partition stable_partition? Divide range in two groups - stable ordering (function template)??? ?? 22.29 本章小結(jié)
?
/* 第22章 變易算法22.1 元素復(fù)制copy22.2 反向復(fù)制copy_backward22.3 元素交換swap22.4 迭代器交換iter_swap22.5 區(qū)間元素交換swap_ranges22.6 元素變換transform22.7 替換Replace22.8 條件替換replace_if22.9 替換和復(fù)制replace_copy22.10 條件替換和復(fù)制replace_copy_if22.11 填充fill22.12 n次填充fill_n22.13 隨機生成元素generate22.14 隨機生成n個元素generate_n22.15 移除復(fù)制remove_copy22.16 條件移除復(fù)制remove_copy_if22.17 移除remove22.18 條件移除remove_if22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy22.20 剔除連續(xù)重復(fù)元素unique22.21 元素反向reverse22.22 反向復(fù)制reverse_copy22.23 旋轉(zhuǎn)rotate22.24 旋轉(zhuǎn)復(fù)制rotate_copy22.25 隨機抖動random_shuffle22.26 隨機采樣random_sample22.27 容器分割partition22.28 容器穩(wěn)定分割stable_partition22.29 本章小結(jié)第22章 變易算法
Modifying sequence operations: 22.1 元素復(fù)制copy
copy Copy range of elements (function template) 22.2 反向復(fù)制copy_backward
copy_backward Copy range of elements backwards (function template) 22.3 元素交換swap
swap Exchange values of two objects (function template) 22.4 迭代器交換iter_swap
iter_swap Exchange values of objects pointed by two iterators (function template) 22.5 區(qū)間元素交換swap_ranges
swap_ranges Exchange values of two ranges (function template) 22.6 元素變換transform
transform Apply function to range (function template) 22.7 替換Replace
replace Replace value in range (function template) 22.8 條件替換replace_if
replace_if Replace values in range (function template) 22.9 替換和復(fù)制replace_copy
replace_copy Copy range replacing value (function template) 22.10 條件替換和復(fù)制replace_copy_if
replace_copy_if Copy range replacing value (function template) 22.11 填充fill
fill Fill range with value (function template) 22.12 n次填充fill_n
fill_n Fill sequence with value (function template) 22.13 隨機生成元素generate
generate Generate values for range with function (function template) 22.14 隨機生成n個元素generate_n
generate_n Generate values for sequence with function (function template) 22.15 移除復(fù)制remove_copy
remove_copy Copy range removing value (function template)22.16 條件移除復(fù)制remove_copy_if
remove_copy_if Copy range removing values (function template)22.17 移除remove
remove Remove value from range (function template)22.18 條件移除remove_if
remove_if Remove elements from range (function template)22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy
unique_copy Copy range removing duplicates (function template)22.20 剔除連續(xù)重復(fù)元素unique
unique Remove consecutive duplicates in range (function template)22.21 元素反向reverse
reverse Reverse range (function template)22.22 反向復(fù)制reverse_copy
reverse_copy Copy range reversed (function template)22.23 旋轉(zhuǎn)rotate
rotate Rotate elements in range (function template)22.24 旋轉(zhuǎn)復(fù)制rotate_copy
rotate_copy Copy rotated range (function template)22.25 隨機抖動random_shuffle
random_shuffle Rearrangle elements in range randomly (function template) 22.26 隨機采樣random_sample
...22.27 容器分割partition
partition Partition range in two (function template) 22.28 容器穩(wěn)定分割stable_partition
stable_partition Divide range in two groups - stable ordering (function template) 22.29 本章小結(jié) */ View Code ?
?
?
?
?
? 第22章 變易算法 Modifying? sequence operations:? ?? 22.1 元素復(fù)制copy copy? Copy range of elements (function template)??
// 第22章 變易算法 // 22.1 元素復(fù)制copy ---------------------------------------------------
template <
class InputIterator,
class OutputIterator>
OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result )
{ while (first!=last) *result++ = *first++
; return result;
} // 把序列一中某范圍內(nèi)的元素,復(fù)制到序列二中去。前兩個參數(shù)是序列一范圍,第三個參數(shù)是序列二的開始位置
// 當序列二不夠大時,不夠裝部分將復(fù)制不過去。 // copy algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[]={
10 ,
20 ,
30 ,
40 ,
50 ,
60 ,
70 };vector <
int >
myvector;vector <
int >
::iterator it;myvector.resize( 7 );
// allocate space for 7 elements // 如7改成5,將只復(fù)制前5個元素 copy ( myints, myints+
7 , myvector.begin() );cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 304
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
int main(
void )
{ // 初始化向量v vector <
int >
v;v.push_back( 1 );v.push_back( 3 );v.push_back( 5 ); // 初始化雙向鏈表l list <
int >
l;l.push_back( 2 );l.push_back( 4 );l.push_back( 6 );l.push_back( 8 );l.push_back( 10 ); // 復(fù)制v到l
copy(v.begin(), v.end(), l.begin()); // 鏈表l打印l 3 5 8 10
for_each(l.begin(), l.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.2 反向復(fù)制copy_backward copy_backward? Copy range of elements backwards (function template)??
// 22.2 反向復(fù)制copy_backward ---------------------------------------------------
template <
class BidirectionalIterator1,
class BidirectionalIterator2>
BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result )
{ while (last!=first) *(--result) = *(--
last); return result;
} // 把序列一中某范圍內(nèi)的元素,復(fù)制到序列二中去。前兩個參數(shù)是序列一范圍,第三個參數(shù)是序列二的結(jié)束位置的下一個位置。
// 兩序列可以是同一序列 // copy_backward example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int >
myvector;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<=
5 ; i++
)myvector.push_back(i *
10 );
// myvector: 10 20 30 40 50
myvector.resize(myvector.size() +
3 );
// allocate space for 3 more elements
copy_backward ( myvector.begin(), myvector.begin() +
5 , myvector.end() );cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 306 始終是左閉右開區(qū)間操作
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std; void print(
int x)
{cout << x <<
' ' ;
} int main(
void )
{vector <
int > v(
10 ); for (unsigned
int i =
0 ; i < v.size(); i++
)v[i] = i +
1 ;
// 1 2 3 4 5 6 7 8 9 10 copy_backward(v.begin(), v.begin() +
3 , v.end());for_each(v.begin(), v.end(), print);cout << endl;
// 1 2 3 4 5 6 7 1 2 3 return 0 ;
} ?
?? 22.3 元素交換swap swap? Exchange values of two objects (function template)??
// 22.3 元素交換swap ---------------------------------------------------
template <
class T>
void swap ( T& a, T&
b )
{T c(a); a =b; b=
c;
}
// 不通過指針交換兩個元素 // swap algorithm example。只要是同一類型的,統(tǒng)統(tǒng)交換
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int x=
10 , y=
20 ;
// x:10 y:20 swap(x,y);
// x:20 y:10
vector <
int > first (
4 ,x), second (
6 ,y);
// first:4x20 second:6x10 swap(first,second);
// first:6x10 second:4x20
cout <<
" first contains: " ; for (vector<
int >::iterator it=first.begin(); it!=first.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 307
#include <algorithm>
#include <iostream>
int main(
void )
{ using namespace std; int a =
5 ; int b =
26 ;cout <<
" 交換前 " <<
" a = " << a <<
" b = " << b <<
endl;swap(a, b);cout <<
" 交換后 " <<
" a = " << a <<
" b = " << b <<
endl; return 0 ;
} ?
?? 22.4 迭代器交換iter_swap iter_swap? Exchange values of objects pointed by two iterators (function template)??
// 22.4 迭代器交換iter_swap ---------------------------------------------------
template <
class ForwardIterator1,
class ForwardIterator2>
void iter_swap ( ForwardIterator1 a, ForwardIterator2 b )
{swap ( *a, *
b);
}
// 利用指針(迭代器)交換值 // iter_swap example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[]={
10 ,
20 ,
30 ,
40 ,
50 };
// myints: 10 20 30 40 50 vector<
int > myvector (
4 ,
99 );
// myvector: 99 99 99 99
iter_swap(myints,myvector.begin()); // myints: [99] 20 30 40 50 // myvector: [10] 99 99 99
iter_swap(myints +
3 ,myvector.begin()+
2 );
// myints: 99 20 30 [99] // myvector: 10 99 [40] 99
cout <<
" myvector contains: " ; for (vector<
int >::iterator it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 307
#include <algorithm>
#include <iostream>
int main(
void )
{ using namespace std; int a =
5 ; int b =
26 ;cout <<
" 交換前 " <<
" a = " << a <<
" b = " << b <<
endl;iter_swap( &a, &
b);cout <<
" 交換后 " <<
" a = " << a <<
" b = " << b <<
endl; return 0 ;
} ?
?? 22.5 區(qū)間元素交換swap_ranges swap_ranges? Exchange values of two ranges (function template)??
// 22.5 區(qū)間元素交換swap_ranges ---------------------------------------------------
template <
class ForwardIterator1,
class ForwardIterator2>
ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2 )
{ while (first1!=last1) swap(*first1++, *first2++
); return first2;
}
// 兩個區(qū)間中的元素分別交換。只有三個參數(shù),第四個參數(shù)又省了
// 注意:第二個序列要足夠長,否則會出現(xiàn)異常,見下面第二個程序。 // swap_ranges example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int > first (
5 ,
10 );
// first: 10 10 10 10 10 vector<
int > second (
5 ,
33 );
// second: 33 33 33 33 33 vector<
int >
::iterator it;swap_ranges(first.begin() +
1 , first.end()-
1 , second.begin()); // print out results of swap: cout <<
" first contains: " ; for (it=first.begin(); it!=first.end(); ++
it)cout <<
" " << *
it;cout <<
" \nsecond contains: " ; for (it=second.begin(); it!=second.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // swap_ranges example. my test:請觀察輸出結(jié)果,當?shù)诙€序列不夠長時,交換到第一個序列的,不是希望的值!
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int > first (
5 ,
10 );
// first: 10 10 10 10 10 vector<
int > second (
2 ,
33 );
// second: 33 33 vector<
int >
::iterator it;swap_ranges(first.begin() +
1 , first.end()-
1 , second.begin()); // print out results of swap: cout <<
" first contains: " ; for (it=first.begin(); it!=first.end(); ++
it)cout <<
" " << *
it;cout <<
" \nsecond contains: " ; for (it=second.begin(); it!=second.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 308
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
} int main(
void )
{vector <
int >
v1, v2;v1.push_back( 1 );v1.push_back( 3 );v1.push_back( 5 );v2.push_back( 2 );v2.push_back( 4 );v2.push_back( 6 ); // 打印v1、v2 cout <<
" 交換前, v1= " ;for_each(v1.begin(), v1.end(), print);cout <<
" v2= " ;for_each(v2.begin(), v2.end(), print);cout <<
endl; // 交換v1、v2
swap_ranges(v1.begin(), v1.end(), v2.begin()); // 打印v1、v2 cout <<
" 交換后, v1= " ;for_each(v1.begin(), v1.end(), print);cout <<
" v2= " ;for_each(v2.begin(), v2.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.6 元素變換transform transform? Apply function to range (function template)??
// 22.6 元素變換transform ---------------------------------------------------
template <
class InputIterator,
class OutputIterator,
class UnaryOperator >
OutputIterator transform ( InputIterator first1, InputIterator last1,OutputIterator result, UnaryOperator op )
{ while (first1 !=
last1) *result++ = op(*first1++);
// or: *result++=binary_op(*first1++,*first2++); return result;
}
// 加轉(zhuǎn)換的copy。而且轉(zhuǎn)換函數(shù)可以是一元的,也可以是二元的。
// 一元的參數(shù)有四個:開始兩個表示第一序列范圍,第三個是結(jié)果存放開始位置,第四個是一元函數(shù)(對象)
// 二元的參數(shù)有五個:開始兩個表示第一序列范圍,第三個是第二序列開始位置,第四個是結(jié)果存放開始位置,第五個是二元函數(shù)(對象) // transform algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int op_increase (
int i) {
return ++
i; }
int op_sum (
int i,
int j) {
return i+
j; } int main () {vector <
int >
first;vector <
int >
second;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
6 ; i++) first.push_back (i*
10 );
// first: 10 20 30 40 50
second.resize(first.size()); // allocate space transform (first.begin(), first.end(), second.begin(), op_increase);
// 四個參數(shù) // second: 11 21 31 41 51
transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // 五個參數(shù) // first: 21 41 61 81 101
cout <<
" first contains: " ; for (it=first.begin(); it!=first.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 309
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std;
int square(
int x)
{ return x *
x;
}
void print(
int x)
{cout << x <<
" " ;
}
int main(
void )
{ // vector初始化 vector <
int >
v;v.push_back( 5 );v.push_back( 15 );v.push_back( 25 ); // list初始化 list <
int > l(
3 ); // 對vector容器元素執(zhí)行平方運算,放入list容器
transform(v.begin(), v.end(), l.begin(), square); // 打印鏈表元素
for_each(l.begin(), l.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.7 替換Replace replace? Replace value in range (function template)??
// 22.7 替換 ---------------------------------------------------
template <
class ForwardIterator,
class T >
void replace ( ForwardIterator first, ForwardIterator last, const T& old_value,
const T&
new_value )
{ for (; first != last; ++
first) if (*first == old_value) *first=
new_value;
}
// 在指定范圍內(nèi)舊值換新值 // replace algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] = {
10 ,
20 ,
30 ,
30 ,
20 ,
10 ,
10 ,
20 };vector <
int > myvector (myints, myints+
8 );
// 10 20 30 30 20 10 10 20
replace (myvector.begin(), myvector.end(), 20 ,
99 );
// 10 99 30 30 99 10 10 99
cout <<
" myvector contains: " ; for (vector<
int >::iterator it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 311
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
int main(
void )
{vector <
int >
v;v.push_back( 13 );v.push_back( 25 );v.push_back( 27 );v.push_back( 25 );v.push_back( 29 ); // 將v的25全部替換為100 replace(v.begin(), v.end(),
25 ,
100 );cout <<
" v向量元素: " ;for_each(v.begin(), v.end(), print);cout <<
endl; // 將iArray的5全部替換為200 int iArray[
7 ] = {
3 ,
6 ,
5 ,
9 ,
5 ,
5 ,
10 };replace(iArray, iArray +
7 ,
5 ,
200 );cout <<
" 數(shù)組iArray元素: " ;for_each(iArray, iArray +
7 , print);cout <<
endl; return 0 ;
} ?
?? 22.8 條件替換replace_if replace_if? Replace values in range (function template)??
// 22.8 條件替換replace_if ---------------------------------------------------
template <
class ForwardIterator,
class Predicate,
class T >
void replace_if ( ForwardIterator first, ForwardIterator last,Predicate pred, const T&
new_value )
{ for (; first != last; ++
first) if (pred(*first)) *first=
new_value;
}
// 當元素滿足什么條件時,更新為新值 // replace_if example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool IsOdd (
int i) {
return ((i%
2 )==
1 ); } int main () {vector <
int >
myvector;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
10 ; i++) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9
replace_if (myvector.begin(), myvector.end(), IsOdd, 0 );
// 0 2 0 4 0 6 0 8 0 // 把奇數(shù)都替換為0 cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 312
#include <algorithm>
#include <vector>
#include <iostream>
bool odd(
int x)
{ return x %
2 ;
}
int main(
void )
{ using namespace std;vector <
int > v(
10 );unsigned int i; for (i =
0 ; i < v.size(); i++
){v[i] = i %
7 ;cout << v[i] <<
' ' ;}cout <<
endl; // 將奇數(shù)元素替換為38 replace_if(v.begin(), v.end(), odd,
38 ); for (i =
0 ; i < v.size(); i++
)cout << v[i] <<
' ' ;cout <<
endl; return 0 ;
} ?
?? 22.9 替換和復(fù)制replace_copy replace_copy? Copy range replacing value (function template)??
// 22.9 替換和復(fù)制replace_copy ---------------------------------------------------
template <
class InputIterator,
class OutputIterator,
class T >
OutputIterator replace_copy ( InputIterator first, InputIterator last,OutputIterator result, const T& old_value,
const T&
new_value )
{ for (; first != last; ++first, ++
result) *result = (*first==old_value)? new_value: *
first; return result;
}
// 將第一序列某范圍內(nèi)的元素,復(fù)制到第二序列中去。在這個復(fù)制過程中,將舊值,替換為新值
// 前兩個參數(shù)表示第一序列范圍,第三個參數(shù)為第二序列開始位置(木有結(jié)束位置),第四、五個參數(shù)為舊、新值 // replace_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] = {
10 ,
20 ,
30 ,
30 ,
20 ,
10 ,
10 ,
20 };vector <
int > myvector (
8 );replace_copy (myints, myints +
8 , myvector.begin(),
20 ,
99 );cout <<
" myvector contains: " ; for (vector<
int >::iterator it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 313
#include <algorithm>
#include <list>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{list <
int >
l1;l1.push_back( 1 );l1.push_back( 3 );l1.push_back( 1 );l1.push_back( 6 );l1.push_back( 8 ); // 將l1鏈表元素1替換為100,然后拷貝到l2鏈表 list <
int > l2(
5 );replace_copy(l1.begin(), l1.end(), l2.begin(), 1 ,
100 );cout <<
" l1保持不變: " ;for_each(l1.begin(), l1.end(), print);cout <<
" \nl2元素為: " ;for_each(l2.begin(), l2.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.10 條件替換和復(fù)制replace_copy_if replace_copy_if? Copy range replacing value (function template)??
// 22.10 條件替換和復(fù)制replace_copy_if ---------------------------------------------------
template <
class InputIterator,
class OutputIterator,
class Predicate,
class T >
OutputIterator replace_copy_if ( InputIterator first, InputIterator last,OutputIterator result, Predicate pred, const T&
new_value )
{ for (; first != last; ++first, ++
result) *result = (pred(*first))? new_value: *
first; return result;
}
// replace_copy的謂詞判斷版本。 木有舊值了,替換為一個謂詞。這樣應(yīng)用范圍更廣 // replace_copy_if example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool IsOdd (
int i) {
return ((i%
2 )==
1 ); } int main () {vector <
int >
first,second;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
10 ; i++) first.push_back(i);
// 1 2 3 4 5 6 7 8 9
second.resize(first.size()); // allocate space replace_copy_if (first.begin(), first.end(), second.begin(), IsOdd,
0 ); // 0 2 0 4 0 6 0 8 0
cout <<
" second contains: " ; for (it=second.begin(); it!=second.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 314
#include <algorithm>
#include <vector>
#include <list>
#include <iostream>
using namespace std; bool odd(
int x)
{ return x %
2 ;
} int main(
void )
{ using namespace std;vector <
int > v(
10 );unsigned int i; for (i =
0 ; i < v.size(); i++
){v[i] = i %
7 ;cout << v[i] <<
' ' ;}cout <<
endl; // 將向量v中奇數(shù)元素替換為38后,拷貝到鏈表l list <
int > l(
10 );replace_copy_if(v.begin(), v.end(), l.begin(), odd, 38 );list <
int >
::iterator list_iter; for (list_iter = l.begin(); list_iter != l.end(); list_iter++
)cout << *list_iter <<
' ' ;cout <<
endl; return 0 ;
} ?
?? 22.11 填充fill fill? Fill range with value (function template)??
// 22.11 填充fill ---------------------------------------------------
template <
class ForwardIterator,
class T >
void fill ( ForwardIterator first, ForwardIterator last,
const T&
value )
{ while (first != last) *first++ =
value;
}
// 在序列范圍內(nèi),均填充上某新值。 // fill algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int > myvector (
8 );
// myvector: 0 0 0 0 0 0 0 0
fill (myvector.begin(),myvector.begin() +
4 ,
5 );
// myvector: 5 5 5 5 0 0 0 0 fill (myvector.begin()+
3 ,myvector.end()-
2 ,
8 );
// myvector: 5 5 5 8 8 8 0 0
cout <<
" myvector contains: " ; for (vector<
int >::iterator it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 315
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
5 );fill(v.begin(), v.end(), 30 );for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.12 n次填充fill_n fill_n? Fill sequence with value (function template)??
// 22.12 n次填充fill_n ---------------------------------------------------
template <
class OutputIterator,
class Size,
class T >
void fill_n ( OutputIterator first, Size n,
const T&
value )
{ for (; n>
0 ; --n) *first++ =
value;
}
// 在起始位置開始,填充n個value。三個參數(shù):起始位置、幾個、新值。 // fill_n example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int > myvector (
8 ,
10 );
// myvector: 10 10 10 10 10 10 10 10
fill_n (myvector.begin(), 4 ,
20 );
// myvector: 20 20 20 20 10 10 10 10 fill_n (myvector.begin()+
3 ,
3 ,
33 );
// myvector: 20 20 20 33 33 33 10 10
cout <<
" myvector contains: " ; for (vector<
int >::iterator it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 315
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
8 );fill(v.begin(), v.end(), 1 ); // 前5個元素填充為2 fill_n(v.begin(),
5 ,
2 );for_each(v.begin(), v.end(), print);cout <<
endl; // 全部填充為3 fill_n(v.begin(), v.size(),
3 );for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.13 隨機生成元素generate generate? Generate values for range with function (function template)??
// 22.13 隨機生成元素generate ---------------------------------------------------
template <
class ForwardIterator,
class Generator>
void generate ( ForwardIterator first, ForwardIterator last, Generator gen )
{ while (first != last) *first++ =
gen();
}
// 在指定范圍內(nèi),填充上第三個參數(shù)gen產(chǎn)生的值。
// gen是個函數(shù)(對象),木有參數(shù)。所以產(chǎn)生的值,純粹取決于gen,與容器原來的元素無關(guān) // generate algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std; // function generator:
int RandomNumber () {
return (rand()%
100 ); }
// 產(chǎn)生一百以內(nèi)的隨機數(shù) // class generator:
struct c_unique { int current;c_unique() {current =
0 ;}
// 構(gòu)造函數(shù),設(shè)成員current初始值為0 int operator ()() {
return ++current;}
// 調(diào)用操作符()重載,使此類能產(chǎn)生函數(shù)對象
} UniqueNumber; int main () {srand ( unsigned ( time(NULL) ) );vector <
int > myvector (
8 );vector <
int >
::iterator it;generate (myvector.begin(), myvector.end(), RandomNumber);cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;generate (myvector.begin(), myvector.end(), UniqueNumber);cout <<
" \nmyvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 316
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
// 等差數(shù)列an+1=an + 3
class sequence
{ public : int a;sequence(){ a =
0 ; }
// 構(gòu)造函數(shù) inline
int operator ()()
// 重載()
{ // a = a + 3; // return a; return a+=
3 ;}
};
void print(
int x)
{cout << x <<
endl;
} int main(
void )
{vector <
int > v(
10 );sequence an;generate(v.begin(), v.end(), an);for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.14 隨機生成n個元素generate_n generate_n? Generate values for sequence with function (function template)??
// 22.14 隨機生成n個元素generate_n ---------------------------------------------------
template <
class OutputIterator,
class Size,
class Generator>
void generate ( OutputIterator first, Size n, Generator gen )
{ for (; n>
0 ; --n) *first++ =
gen();
}
// 在開始位置之后,填充n個gen()產(chǎn)生的值。三個參數(shù):起始位置、幾個、gen產(chǎn)生的值。 // generate_n example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int current(
0 );
int UniqueNumber () {
return ++
current; } int main () {vector <
int > myvector (
9 );vector <
int >
::iterator it;generate_n (myvector.begin(), 6 , UniqueNumber);
// 因為產(chǎn)生6個,后面幾個仍是0
cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 317
#include <algorithm>
#include <vector>
#include <iostream>
int main(
void )
{ using namespace std;vector <
int > v(
10 ); // 生成3個偽隨機數(shù) generate_n(v.begin(),
3 , rand); for (unsigned
int i =
0 ; i < v.size(); i++
)cout << v[i] <<
' ' ;cout <<
endl; return 0 ;
} ?
?? 22.15 移除復(fù)制remove_copy remove_copy? Copy range removing value (function template)
// 22.15 移除復(fù)制remove_copy ---------------------------------------------------
template <
class InputIterator,
class OutputIterator,
class T>
OutputIterator remove_copy ( InputIterator first, InputIterator last,OutputIterator result, const T&
value )
{ for ( ; first != last; ++
first) if (!(*first == value)) *result++ = *
first; return result;
}
// 將容器中,不等于某值的元素,復(fù)制到另一容器中。
// 參數(shù)有四個:前兩個表示范圍,第三個是另一容器的起始位置,第四個是某值。
// 這樣就產(chǎn)生了一個問題,另一容器,可以是同一容器么?可以的,請看下面第二個my test程序。 // remove_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] = {
10 ,
20 ,
30 ,
30 ,
20 ,
10 ,
10 ,
20 };
// 10 20 30 30 20 10 10 20 vector<
int > myvector (
8 );
// 如改成2,可以看到,容器不會自動擴充,只會復(fù)制進2個數(shù)。 vector<
int >
::iterator it;remove_copy (myints,myints +
8 ,myvector.begin(),
20 );
// 10 30 30 10 10 0 0 0
cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // remove_copy example。my test: 同一容器是可以的。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] = {
10 ,
20 ,
30 ,
30 ,
20 ,
10 ,
10 ,
20 };
// 10 20 30 30 20 10 10 20 vector<
int > myvector (myints,myints+
8 );vector <
int >
::iterator it; // 10 30 30 10 10 [10 10 20 remove_copy (myvector.begin(),myvector.end(),myvector.begin(),
20 );
// 最后三個是原來的
cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 318
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
int main(
void )
{vector <
int >
v;v.push_back( 2 );v.push_back( 4 );v.push_back( 3 );v.push_back( 4 );v.push_back( 8 ); //
int iArray[
6 ]={
0 }; //
remove_copy(v.begin(), v.end(), iArray,
4 );for_each(v.begin(), v.end(), print);cout <<
endl; //
for_each(iArray, iArray +
6 , print);cout <<
endl; return 0 ;
} ?
?? 22.16 條件移除復(fù)制remove_copy_if remove_copy_if? Copy range removing values (function template)
// 22.16 條件移除復(fù)制remove_copy_if ---------------------------------------------------
template <
class InputIterator,
class OutputIterator,
class Predicate>
OutputIterator remove_copy_if ( InputIterator first, InputIterator last,OutputIterator result, Predicate pred )
{ for ( ; first != last; ++
first) if (!pred(*first)) *result++ = *
first; return result;
}
// remove_copy 的帶謂詞判斷的版本。把第四個參數(shù),改成了函數(shù)(對象)。 // remove_copy_if example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool IsOdd (
int i) {
return ((i%
2 )==
1 ); } int main () { int myints[] = {
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 }; vector <
int > myvector (
9 );vector <
int >
::iterator it;remove_copy_if (myints,myints +
9 ,myvector.begin(),IsOdd);
// 是奇數(shù)的,被刪除
cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 319
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
bool even(
int x)
{ // 偶數(shù) return x %
2 ?
0 :
1 ;
}
int main(
void )
{ // 初始化向量v vector <
int >
v;v.push_back( 7 );v.push_back( 2 );v.push_back( 5 );v.push_back( 4 );v.push_back( 1 ); // 初始化數(shù)組iArray int iArray[
6 ] = {
0 }; // 移除v中偶數(shù),剩余元素復(fù)制到iArray
remove_copy_if(v.begin(), v.end(), iArray, even); // 打印v,v沒有改變
for_each(v.begin(), v.end(), print);cout <<
endl; // 打印iArray for_each(iArray, iArray +
6 , print);cout <<
endl; return 0 ;
} ?
?? 22.17 移除remove remove? Remove value from range (function template)
// 22.17 移除remove ---------------------------------------------------
template <
class ForwardIterator,
class T >
ForwardIterator remove ( ForwardIterator first, ForwardIterator last, const T&
value )
{ForwardIterator result =
first; for ( ; first != last; ++
first) if (!(*first == value)) *result++ = *
first; return result;
}
// 將容器中等于某值的元素刪除。
// 因為在同一容器中操作,所以返回值值得關(guān)注。返回排列完刪除某些元素之后的序列的后一個位置。
// 如果要將數(shù)據(jù)復(fù)制到另一容器,可以考慮用remove_copy // remove algorithm example
#include <iostream>
#include <algorithm>
using namespace std; 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 = remove (pbegin, pend,
20 );
// 10 30 30 10 10 10 10 20 // ^ ^ cout <<
" range contains: " ; for (
int * p=pbegin; p!=pend; ++
p)cout <<
" " << *
p;cout <<
endl; return 0 ;
} // 321
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
int main(
void )
{ // 初始化向量v vector <
int >
v;v.push_back( 2 );v.push_back( 4 );v.push_back( 3 );v.push_back( 4 );v.push_back( 8 ); // 移除4 vector <
int > ::iterator result = remove(v.begin(), v.end(),
4 ); // 打印2 3 8
for_each(v.begin(), result, print);cout <<
endl; // 打印2 3 8 4 8
for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.18 條件移除remove_if remove_if? Remove elements from range (function template)
// 22.18 條件移除remove_if ---------------------------------------------------
template <
class ForwardIterator,
class Predicate >
ForwardIterator remove_if ( ForwardIterator first, ForwardIterator last,Predicate pred )
{ForwardIterator result =
first; for ( ; first != last; ++
first) if (!pred(*first)) *result++ = *
first; return result;
}
// remove的謂詞判斷版本。 remove的第三個參數(shù),改成了謂詞判斷。 // remove_if example
#include <iostream>
#include <algorithm>
using namespace std; 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 = remove_if (pbegin, pend, IsOdd);
// 2 4 6 8 5 6 7 8 9 // ^ ^ cout <<
" range contains: " ; for (
int * p=pbegin; p!=pend; ++
p)cout <<
" " << *
p;cout <<
endl; return 0 ;
} // 322
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
" " ;
}
bool even(
int x)
{ // 偶數(shù) return x %
2 ?
0 :
1 ;
}
int main(
void )
{ // 初始化向量v vector <
int >
v;v.push_back( 7 );v.push_back( 2 );v.push_back( 5 );v.push_back( 4 );v.push_back( 1 ); // 移除偶數(shù) vector <
int > ::iterator result =
remove_if(v.begin(), v.end(), even); // 打印7 5 1
for_each(v.begin(), result, print);cout <<
endl; // 打印7 5 1 4 1
for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy unique_copy? Copy range removing duplicates (function template)
// 22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy ---------------------------------------------------
template <
class InputIterator,
class OutputIterator>
OutputIterator unique_copy ( InputIterator first, InputIterator last,OutputIterator result )
{ *result=*
first; while (++first !=
last){ if (!(*result == *first))
// or: if (!pred(*result,*first)) for the pred version *(++result)=*first;
// 有點精巧,畫圖可明白。pred左參數(shù),是左邊那個與當前值不同那個元素,右參數(shù)是當前元素
} return ++result;
// 左閉右開原則,返回時要+1
}
// 將連續(xù)相等的元素過濾掉,只剩下一個
// 第四個參數(shù)可選,如有,是謂詞判斷的函數(shù)(對象)。這個謂詞估計不常用。
// 第三個參數(shù)是輸出的第一個位置。可以用同一容器,也可以用不同容器。 // unique_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool myfunction (
int i,
int j) { return (i==
j);
} int main () { int myints[] = {
10 ,
20 ,
20 ,
20 ,
30 ,
30 ,
20 ,
20 ,
10 };vector <
int > myvector (
9 );
// 0 0 0 0 0 0 0 0 0 vector<
int >
::iterator it; // using default comparison: it=unique_copy (myints,myints+
9 ,myvector.begin());
// 10 20 30 20 10 0 0 0 0 // ^
sort (myvector.begin(),it); // 10 10 20 20 30 0 0 0 0 // ^ // using predicate comparison: it=
unique_copy (myvector.begin(), it, myvector.begin(), myfunction); // 10 20 30 20 30 0 0 0 0 // ^
myvector.resize( it - myvector.begin() );
// 10 20 30 // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 323
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int >
v;v.push_back( 2 );v.push_back( 5 );v.push_back( 5 );v.push_back( 5 );v.push_back( 6 );v.push_back( 5 );v.push_back( 2 ); //
int iArray[
6 ] = {
0 }; //
unique_copy(v.begin(), v.end(), iArray); // 打印2 5 6 5 2 0 for_each(iArray, iArray +
6 , print);cout <<
endl; return 0 ;
} ?
?? 22.20 剔除連續(xù)重復(fù)元素unique unique? Remove consecutive duplicates in range (function template)
// 22.20 剔除連續(xù)重復(fù)元素unique ---------------------------------------------------
template <
class ForwardIterator>
ForwardIterator unique ( ForwardIterator first, ForwardIterator last )
{ForwardIterator result =
first; while (++first !=
last){ if (!(*result == *first))
// or: if (!pred(*result,*first)) for the pred version *(++result)=*
first;} return ++
result;
}
// 將連續(xù)相等的元素過濾掉,只剩下一個。在同一容器操作。
// 第三個參數(shù)可無,如有是謂詞判斷函數(shù)(對象) // unique algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; 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 vector<
int > myvector (myints,myints+
9 );vector <
int >
::iterator it; // using default comparison: it = unique (myvector.begin(), myvector.end());
// 10 20 30 20 10 30 20 20 10 // ^
myvector.resize( it - myvector.begin() );
// 10 20 30 20 10 // using predicate comparison: unique (myvector.begin(), myvector.end(), myfunction);
// (no changes) // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 325
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int >
v;v.push_back( 2 );v.push_back( 6 );v.push_back( 6 );v.push_back( 6 );v.push_back( 9 );v.push_back( 6 );v.push_back( 3 ); //
vector <
int > ::iterator result =
unique(v.begin(), v.end()); // 打印2 6 9 6 3
for_each(v.begin(), result, print);cout <<
endl; // 打印2 6 9 6 3 6 3
for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.21 元素反向reverse reverse? Reverse range (function template)
// 22.21 元素反向reverse ---------------------------------------------------
template <
class BidirectionalIterator>
void reverse ( BidirectionalIterator first, BidirectionalIterator last)
{ while ((first!=last)&&(first!=--
last))swap ( *first++,*
last);
}
// 反轉(zhuǎn)元素 // reverse algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int >
myvector;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
10 ; ++i) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9
reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1 // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 326
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
} int main(
void )
{vector <
int > v(
10 ); for (unsigned
int i =
0 ; i < v.size(); i++
)v[i] =
i;for_each(v.begin(), v.end(), print);cout <<
endl; //
reverse(v.begin(), v.end());for_each(v.begin(), v.end(), print);cout <<
endl; return 0 ;
} ?
?? 22.22 反向復(fù)制reverse_copy reverse_copy? Copy range reversed (function template)
// 22.22 反向復(fù)制reverse_copy ---------------------------------------------------
template <
class BidirectionalIterator,
class OutputIterator>
OutputIterator reverse_copy ( BidirectionalIterator first,BidirectionalIterator last, OutputIterator result )
{ while (first!=last) *result++ = *--
last; return result;
} // 從后向前,復(fù)制到另一容器。
// 復(fù)制到同一容器,可以嗎? 請見以下第二個程序,my test。
// my test:復(fù)制到同一空間(范圍),會產(chǎn)生沖突。復(fù)制到同一容器不同范圍,木有關(guān)系。 // reverse_copy example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] ={
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 };vector <
int >
myvector;vector <
int >
::iterator it;myvector.resize( 9 );reverse_copy (myints, myints +
9 , myvector.begin()); // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // reverse_copy example。 my test:復(fù)制到同一空間(范圍),會產(chǎn)生沖突。復(fù)制到同一容器不同范圍,木有關(guān)系。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] ={
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 };vector <
int > v(myints,myints+
9 );vector <
int >
::iterator it;reverse_copy(v.begin(),v.begin() +
3 ,v.begin()); // print out content: cout <<
" myvector contains: " ; for (it=v.begin(); it!=v.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 327
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
10 ); for (unsigned
int i =
0 ; i < v.size(); i++
)v[i] =
i; //
int iArray[
10 ] = {
0 }; //
reverse_copy(v.begin(), v.end(), iArray);for_each(iArray, iArray +
10 , print);cout <<
endl; return 0 ;
} ?
?? 22.23 旋轉(zhuǎn)rotate rotate? Rotate elements in range (function template)
// 22.23 旋轉(zhuǎn)rotate ---------------------------------------------------
template <
class ForwardIterator>
void rotate ( ForwardIterator first, ForwardIterator middle,ForwardIterator last )
{ForwardIterator next =
middle; while (first!=
next){swap ( *first++,*next++
); if (next==last) next=
middle; else if (first == middle) middle=next;
// 這種交換方法,非常新穎??梢援媹D理解
}
}
// 旋轉(zhuǎn)交換。[first,middle) 與 [middle,last),兩區(qū)域的元素,進行交換。兩區(qū)域的元素個數(shù)不一定要相等。
// 如 1,2,3,4,5 ,以4為middle,--> 4,5,1,2,3 。 三個參數(shù):first, middle, last // rotate algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () {vector <
int >
myvector;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
10 ; ++i) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9
rotate(myvector.begin(),myvector.begin() +
3 ,myvector.end()); // 4 5 6 7 8 9 1 2 3 // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 329
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
10 );unsigned int i; for (i =
0 ; i < v.size(); i++
)v[i] = i +
1 ;for_each(v.begin(), v.end(), print);cout <<
endl; //
cout <<
" 開始旋轉(zhuǎn),middle元素= " << *(v.begin() +
7 ) <<
endl;rotate(v.begin(), v.begin() +
7 , v.end());for_each(v.begin(), v.end(), print); return 0 ;
} ?
?? 22.24 旋轉(zhuǎn)復(fù)制rotate_copy rotate_copy? Copy rotated range (function template)
// 22.24 旋轉(zhuǎn)復(fù)制rotate_copy ---------------------------------------------------
template <
class ForwardIterator,
class OutputIterator>
OutputIterator rotate_copy ( ForwardIterator first, ForwardIterator middle,ForwardIterator last, OutputIterator result )
{result =
copy (middle,last,result); return copy (first,middle,result);
}
// 與rotate類似,無非是拷貝到了另一容器。四個參數(shù):first, middle, last, result // rotate_copy algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; int main () { int myints[] = {
10 ,
20 ,
30 ,
40 ,
50 ,
60 ,
70 };vector <
int >
myvector;vector <
int >
::iterator it;myvector.resize( 7 );rotate_copy(myints,myints +
3 ,myints+
7 ,myvector.begin()); // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 329
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
10 ); for (unsigned
int i =
0 ; i < v.size(); i++
)v[i] = i +
1 ; //
int iArray[
10 ] ={
0 }; //
cout <<
" 開始旋轉(zhuǎn)復(fù)制,*middle選為 " << *(v.begin() +
7 ) <<
endl;rotate_copy(v.begin(), v.begin() +
7 , v.end(), iArray);for_each(iArray, iArray +
10 , print);cout <<
endl; return 0 ;
} ?
?? 22.25 隨機抖動random_shuffle random_shuffle? Rearrangle elements in range randomly (function template)??
// 22.25 隨機抖動random_shuffle ---------------------------------------------------
template <
class RandomAccessIterator,
class RandomNumberGenerator>
void random_shuffle ( RandomAccessIterator first, RandomAccessIterator last,RandomNumberGenerator &
rand )
{iterator_traits <RandomAccessIterator>
::difference_type i, n;n = (last-
first); for (i=
2 ; i<n; ++
i) swap (first[i],first[rand(i)]);
}
// shuffle 洗牌。第三個參數(shù)可無,如有,是自定義的隨機發(fā)生器。 // random_shuffle example
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <ctime>
#include <cstdlib>
using namespace std; // random generator function:
ptrdiff_t myrandom (ptrdiff_t i) {
return rand()%
i;} // pointer object to it:
ptrdiff_t (*p_myrandom)(ptrdiff_t) =
myrandom; int main () {srand ( unsigned ( time (NULL) ) );vector <
int >
myvector;vector <
int >
::iterator it; // set some values: for (
int i=
1 ; i<
10 ; ++i) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9 // using built-in random generator:
random_shuffle ( myvector.begin(), myvector.end() ); // using myrandom:
random_shuffle ( myvector.begin(), myvector.end(), p_myrandom); // print out content: cout <<
" myvector contains: " ; for (it=myvector.begin(); it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 331
#include <algorithm>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{ int iArray[
10 ] = {
1 ,
2 ,
3 ,
4 ,
5 ,
6 ,
7 ,
8 ,
9 ,
10 };cout <<
" 隨機抖動iArray三次 " <<
endl; for (
int i =
0 ; i <
3 ; i++
){random_shuffle(iArray, iArray +
10 );for_each(iArray, iArray +
10 , print);cout <<
endl;} return 0 ;
} ?
?? 22.26 隨機采樣random_sample ...
// 22.26 隨機采樣random_sample --------------------------------------------------- // 333 。random_sample不是標準函數(shù)。我們可以采用另兩個函數(shù),來達到相同的目的:
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
int main(
void )
{vector <
int > v(
10 ); for (unsigned
int i =
0 ; i < v.size(); i++
)v[i] = i %
8 ; //
cout <<
" v= " ;for_each(v.begin(), v.end(), print);cout <<
endl; //
const int n =
6 ;
// 采樣個數(shù) int iArray[n]; // random_sample(v.begin(), v.end(), iArray, iArray + n);
random_shuffle(v.begin(),v.end());copy(v.begin(),v.begin() +
n,iArray); //
cout << n <<
" 個采樣元素為 " ;for_each(iArray, iArray +
n, print);cout <<
endl; return 0 ;
} ?
?? 22.27 容器分割partition partition? Partition range in two (function template)??
// 22.27 容器分割partition ---------------------------------------------------
template <
class BidirectionalIterator,
class Predicate>
BidirectionalIterator partition ( BidirectionalIterator first,BidirectionalIterator last, Predicate pred )
{ while (first!=last)
// 可以看到快排的影子
{ --
last; while (first!=last && pred(*first)) ++
first; while (first!=last && !pred(*last)) --
last; if (first!=last) swap (*first++,*
last);} return first;
}
// 把序列分成兩部分,前一部分滿足條件,后一部分不滿足條件。三個參數(shù)。 // partition algorithm example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool IsOdd (
int i) {
return (i%
2 )==
1 ; } int main () {vector <
int >
myvector;vector <
int >
::iterator it, bound; // set some values: for (
int i=
1 ; i<
10 ; ++i) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9
bound =
partition (myvector.begin(), myvector.end(), IsOdd); // print out content: cout <<
" odd members: " ; for (it=myvector.begin(); it!=bound; ++
it)cout <<
" " << *
it;cout <<
" \neven members: " ; for (it=bound; it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 334
#include <algorithm>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
bool less10(
int x)
{ return x <
10 ;
}
int main(
void )
{ int iArray[
10 ] = {
16 , -
1 ,
3 ,
11 ,
2 ,
5 ,
8 ,
20 ,
9 ,
3 }; int *result = partition(iArray, iArray +
10 , less10);cout <<
" 按小于10進行分割 " <<
endl;for_each(iArray, iArray +
10 , print);cout <<
endl; //
cout <<
" partition算法返回的分界元素為 " << *result <<
endl; return 0 ;
} ?
?? 22.28 容器穩(wěn)定分割stable_partition stable_partition? Divide range in two groups - stable ordering (function template)??
// 22.28 容器穩(wěn)定分割stable_partition --------------------------------------------------- // partition 的穩(wěn)定版本 // stable_partition example
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std; bool IsOdd (
int i) {
return (i%
2 )==
1 ; } int main () {vector <
int >
myvector;vector <
int >
::iterator it, bound; // set some values: for (
int i=
1 ; i<
10 ; ++i) myvector.push_back(i);
// 1 2 3 4 5 6 7 8 9
bound =
stable_partition (myvector.begin(), myvector.end(), IsOdd); // print out content: cout <<
" odd members: " ; for (it=myvector.begin(); it!=bound; ++
it)cout <<
" " << *
it;cout <<
" \neven members: " ; for (it=bound; it!=myvector.end(); ++
it)cout <<
" " << *
it;cout <<
endl; return 0 ;
} // 338
#include <algorithm>
#include <iostream>
using namespace std;
void print(
int x)
{cout << x <<
' ' ;
}
bool less10(
int x)
{ return x <
10 ?
1 :
0 ;
}
int main(
void )
{ int iArray[
10 ] = {
16 , -
1 ,
3 ,
11 ,
2 ,
5 ,
8 ,
20 ,
9 ,
3 };for_each(iArray, iArray +
10 , print);cout <<
endl; // 進行穩(wěn)定分割 int *result = stable_partition(iArray, iArray +
10 , less10);cout <<
" 按小于10穩(wěn)定分割,iArray數(shù)組變?yōu)?/span>" << endl;for_each(iArray, iArray + 10 , print);cout << endl; //
cout << " iArray數(shù)組有如下小于10的整數(shù) " << endl;for_each(iArray, result, print);cout << endl; //
cout << " stable_partition算法返回的分界元素為 " << *result << endl; return 0 ;
} ?
?? 22.29 本章小結(jié)
?
/* **************************************************************************************************************// 22.29 本章小結(jié) ---------------------------------------------------22.1 元素復(fù)制copy
// 把序列一中某范圍內(nèi)的元素,復(fù)制到序列二中去。前兩個參數(shù)是序列一范圍,第三個參數(shù)是序列二的開始位置
// 當序列二不夠大時,不夠裝部分將復(fù)制不過去。22.2 反向復(fù)制copy_backward
// 把序列一中某范圍內(nèi)的元素,復(fù)制到序列二中去。前兩個參數(shù)是序列一范圍,第三個參數(shù)是序列二的結(jié)束位置的下一個位置。
// 兩序列可以是同一序列22.3 元素交換swap
// 不通過指針交換兩個元素22.4 迭代器交換iter_swap
// 利用指針(迭代器)交換值22.5 區(qū)間元素交換swap_ranges
// 兩個區(qū)間中的元素分別交換。只有三個參數(shù),第四個參數(shù)又省了
// 注意:第二個序列要足夠長,否則會出現(xiàn)異常。22.6 元素變換transform
// 加轉(zhuǎn)換的copy。而且轉(zhuǎn)換函數(shù)可以是一元的,也可以是二元的。
// 一元的參數(shù)有四個:開始兩個表示第一序列范圍,第三個是結(jié)果存放開始位置,第四個是一元函數(shù)(對象)
// 二元的參數(shù)有五個:開始兩個表示第一序列范圍,第三個是第二序列開始位置,第四個是結(jié)果存放開始位置,第五個是二元函數(shù)(對象)22.7 替換Replace
// 在指定范圍內(nèi)舊值換新值22.8 條件替換replace_if
// 當元素滿足什么條件時,更新為新值22.9 替換和復(fù)制replace_copy
// 將第一序列某范圍內(nèi)的元素,復(fù)制到第二序列中去。在這個復(fù)制過程中,將舊值,替換為新值
// 前兩個參數(shù)表示第一序列范圍,第三個參數(shù)為第二序列開始位置(木有結(jié)束位置),第四、五個參數(shù)為舊、新值22.10 條件替換和復(fù)制replace_copy_if
// replace_copy的謂詞判斷版本。 木有舊值了,替換為一個謂詞。這樣應(yīng)用范圍更廣22.11 填充fill
// 在序列范圍內(nèi),均填充上某新值。22.12 n次填充fill_n
// 在起始位置開始,填充n個value。三個參數(shù):起始位置、幾個、新值。22.13 隨機生成元素generate
// 在指定范圍內(nèi),填充上第三個參數(shù)gen產(chǎn)生的值。
// gen是個函數(shù)(對象),木有參數(shù)。所以產(chǎn)生的值,純粹取決于gen,與容器原來的元素無關(guān)22.14 隨機生成n個元素generate_n
// 在開始位置之后,填充n個gen()產(chǎn)生的值。三個參數(shù):起始位置、幾個、gen產(chǎn)生的值。22.15 移除復(fù)制remove_copy
// 將容器中,不等于某值的元素,復(fù)制到另一容器中。
// 參數(shù)有四個:前兩個表示范圍,第三個是另一容器的起始位置,第四個是某值。
// 這樣就產(chǎn)生了一個問題,另一容器,可以是同一容器么?可以的。22.16 條件移除復(fù)制remove_copy_if
// remove_copy 的帶謂詞判斷的版本。把第四個參數(shù),改成了函數(shù)(對象)。22.17 移除remove
// 將容器中等于某值的元素刪除。
// 因為在同一容器中操作,所以返回值值得關(guān)注。返回排列完刪除某些元素之后的序列的后一個位置。
// 如果要將數(shù)據(jù)復(fù)制到另一容器,可以考慮用remove_copy22.18 條件移除remove_if
// remove的謂詞判斷版本。 remove的第三個參數(shù),改成了謂詞判斷。22.19 不連續(xù)重復(fù)元素復(fù)制unique_copy
// 將連續(xù)相等的元素過濾掉,只剩下一個
// 第四個參數(shù)可選,如有,是謂詞判斷的函數(shù)(對象)。這個謂詞估計不常用。
// 第三個參數(shù)是輸出的第一個位置??梢杂猛蝗萜?#xff0c;也可以用不同容器。22.20 剔除連續(xù)重復(fù)元素unique
// 將連續(xù)相等的元素過濾掉,只剩下一個。在同一容器操作。
// 第三個參數(shù)可無,如有是謂詞判斷函數(shù)(對象)22.21 元素反向reverse
// 反轉(zhuǎn)元素22.22 反向復(fù)制reverse_copy
// 從后向前,復(fù)制到另一容器。
// 復(fù)制到同一容器,可以嗎? 請見以下第二個程序,my test。
// my test:復(fù)制到同一空間(范圍),會產(chǎn)生沖突。復(fù)制到同一容器不同范圍,木有關(guān)系。22.23 旋轉(zhuǎn)rotate
// 旋轉(zhuǎn)交換。[first,middle) 與 [middle,last),兩區(qū)域的元素,進行交換。兩區(qū)域的元素個數(shù)不一定要相等。
// 如 1,2,3,4,5 ,以4為middle,--> 4,5,1,2,3 。 三個參數(shù):first, middle, last22.24 旋轉(zhuǎn)復(fù)制rotate_copy
// 與rotate類似,無非是拷貝到了另一容器。四個參數(shù):first, middle, last, result22.25 隨機抖動random_shuffle
// shuffle 洗牌。第三個參數(shù)可無,如有,是自定義的隨機發(fā)生器。22.26 隨機采樣random_sample
//random_sample不是標準函數(shù)。我們可以采用另兩個函數(shù),來達到相同的目的//random_sample(v.begin(), v.end(), iArray, iArray + n);random_shuffle(v.begin(),v.end());copy(v.begin(),v.begin()+n,iArray);22.27 容器分割partition
// 把序列分成兩部分,前一部分滿足條件,后一部分不滿足條件。三個參數(shù)。22.28 容器穩(wěn)定分割stable_partition
// partition 的穩(wěn)定版本22.29 本章小結(jié)
// 這就是小結(jié)********************************************************************************************************** */ ?
?
?
?
?
??
?
?
00TOP00
?
轉(zhuǎn)載于:https://www.cnblogs.com/xin-le/p/4115586.html
總結(jié)
以上是生活随笔 為你收集整理的第22章 变易算法 的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔 推薦給好友。