日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

第22章 变易算法

發布時間:2025/7/25 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 第22章 变易算法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

?

?

第22章 變易算法? Modifying? sequence operations


?? 22.1 元素復制copy
copy? Copy range of elements (function template)???
?? 22.2 反向復制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 區間元素交換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 替換和復制replace_copy
replace_copy? Copy range replacing value (function template)???
?? 22.10 條件替換和復制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 移除復制remove_copy
remove_copy? Copy range removing value (function template)
?? 22.16 條件移除復制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 不連續重復元素復制unique_copy
unique_copy? Copy range removing duplicates (function template)
?? 22.20 剔除連續重復元素unique
unique? Remove consecutive duplicates in range (function template)
?? 22.21 元素反向reverse
reverse? Reverse range (function template)
?? 22.22 反向復制reverse_copy
reverse_copy? Copy range reversed (function template)
?? 22.23 旋轉rotate
rotate? Rotate elements in range (function template)
?? 22.24 旋轉復制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 容器穩定分割stable_partition
stable_partition? Divide range in two groups - stable ordering (function template)???
?? 22.29 本章小結

?

/*第22章 變易算法22.1 元素復制copy22.2 反向復制copy_backward22.3 元素交換swap22.4 迭代器交換iter_swap22.5 區間元素交換swap_ranges22.6 元素變換transform22.7 替換Replace22.8 條件替換replace_if22.9 替換和復制replace_copy22.10 條件替換和復制replace_copy_if22.11 填充fill22.12 n次填充fill_n22.13 隨機生成元素generate22.14 隨機生成n個元素generate_n22.15 移除復制remove_copy22.16 條件移除復制remove_copy_if22.17 移除remove22.18 條件移除remove_if22.19 不連續重復元素復制unique_copy22.20 剔除連續重復元素unique22.21 元素反向reverse22.22 反向復制reverse_copy22.23 旋轉rotate22.24 旋轉復制rotate_copy22.25 隨機抖動random_shuffle22.26 隨機采樣random_sample22.27 容器分割partition22.28 容器穩定分割stable_partition22.29 本章小結第22章 變易算法 Modifying sequence operations: 22.1 元素復制copy copy Copy range of elements (function template) 22.2 反向復制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 區間元素交換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 替換和復制replace_copy replace_copy Copy range replacing value (function template) 22.10 條件替換和復制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 移除復制remove_copy remove_copy Copy range removing value (function template)22.16 條件移除復制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 不連續重復元素復制unique_copy unique_copy Copy range removing duplicates (function template)22.20 剔除連續重復元素unique unique Remove consecutive duplicates in range (function template)22.21 元素反向reverse reverse Reverse range (function template)22.22 反向復制reverse_copy reverse_copy Copy range reversed (function template)22.23 旋轉rotate rotate Rotate elements in range (function template)22.24 旋轉復制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 容器穩定分割stable_partition stable_partition Divide range in two groups - stable ordering (function template) 22.29 本章小結*/ View Code

?

?

?


?

?

? 第22章 變易算法
Modifying? sequence operations:?
?? 22.1 元素復制copy
copy? Copy range of elements (function template)??

// 第22章 變易算法// 22.1 元素復制copy --------------------------------------------------- template<class InputIterator, class OutputIterator>OutputIterator copy ( InputIterator first, InputIterator last, OutputIterator result ) {while (first!=last) *result++ = *first++;return result; }// 把序列一中某范圍內的元素,復制到序列二中去。前兩個參數是序列一范圍,第三個參數是序列二的開始位置 // 當序列二不夠大時,不夠裝部分將復制不過去。// 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,將只復制前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) {//初始化向量vvector < int > v;v.push_back(1);v.push_back(3);v.push_back(5);//初始化雙向鏈表llist < int > l;l.push_back(2);l.push_back(4);l.push_back(6);l.push_back(8);l.push_back(10);//復制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 反向復制copy_backward
copy_backward? Copy range of elements backwards (function template)??

// 22.2 反向復制copy_backward --------------------------------------------------- template<class BidirectionalIterator1, class BidirectionalIterator2>BidirectionalIterator2 copy_backward ( BidirectionalIterator1 first,BidirectionalIterator1 last,BidirectionalIterator2 result ) {while (last!=first) *(--result) = *(--last);return result; }// 把序列一中某范圍內的元素,復制到序列二中去。前兩個參數是序列一范圍,第三個參數是序列二的結束位置的下一個位置。 // 兩序列可以是同一序列// 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 始終是左閉右開區間操作 #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 10copy_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 3return 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。只要是同一類型的,統統交換 #include <iostream> #include <algorithm> #include <vector> using namespace std;int main () {int x=10, y=20; // x:10 y:20swap(x,y); // x:20 y:10 vector<int> first (4,x), second (6,y); // first:4x20 second:6x10swap(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 50vector<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 區間元素交換swap_ranges
swap_ranges? Exchange values of two ranges (function template)??

// 22.5 區間元素交換swap_ranges --------------------------------------------------- template<class ForwardIterator1, class ForwardIterator2>ForwardIterator2 swap_ranges ( ForwardIterator1 first1, ForwardIterator1 last1,ForwardIterator2 first2 ) {while (first1!=last1) swap(*first1++, *first2++);return first2; } // 兩個區間中的元素分別交換。只有三個參數,第四個參數又省了 // 注意:第二個序列要足夠長,否則會出現異常,見下面第二個程序。// swap_ranges example #include <iostream> #include <algorithm> #include <vector> using namespace std;int main () {vector<int> first (5,10); // first: 10 10 10 10 10vector<int> second (5,33); // second: 33 33 33 33 33vector<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:請觀察輸出結果,當第二個序列不夠長時,交換到第一個序列的,不是希望的值! #include <iostream> #include <algorithm> #include <vector> using namespace std;int main () {vector<int> first (5,10); // first: 10 10 10 10 10vector<int> second (2,33); // second: 33 33vector<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、v2cout << "交換前, 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、v2cout << "交換后, 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; } // 加轉換的copy。而且轉換函數可以是一元的,也可以是二元的。 // 一元的參數有四個:開始兩個表示第一序列范圍,第三個是結果存放開始位置,第四個是一元函數(對象) // 二元的參數有五個:開始兩個表示第一序列范圍,第三個是第二序列開始位置,第四個是結果存放開始位置,第五個是二元函數(對象)// 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 spacetransform (first.begin(), first.end(), second.begin(), op_increase); // 四個參數// second: 11 21 31 41 51 transform (first.begin(), first.end(), second.begin(), first.begin(), op_sum); // 五個參數// 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容器元素執行平方運算,放入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; } // 在指定范圍內舊值換新值// 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全部替換為100replace(v.begin(), v.end(), 25, 100);cout << "v向量元素: ";for_each(v.begin(), v.end(), print);cout << endl;//將iArray的5全部替換為200int iArray[7] = {3, 6, 5, 9, 5, 5, 10};replace(iArray, iArray + 7, 5, 200);cout << "數組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// 把奇數都替換為0cout << "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;//將奇數元素替換為38replace_if(v.begin(), v.end(), odd, 38);for(i = 0; i < v.size(); i++)cout << v[i] << ' ';cout << endl;return 0; }

?


?? 22.9 替換和復制replace_copy
replace_copy? Copy range replacing value (function template)??

// 22.9 替換和復制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; } // 將第一序列某范圍內的元素,復制到第二序列中去。在這個復制過程中,將舊值,替換為新值 // 前兩個參數表示第一序列范圍,第三個參數為第二序列開始位置(木有結束位置),第四、五個參數為舊、新值// 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 條件替換和復制replace_copy_if
replace_copy_if? Copy range replacing value (function template)??

// 22.10 條件替換和復制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的謂詞判斷版本。 木有舊值了,替換為一個謂詞。這樣應用范圍更廣// 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 spacereplace_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中奇數元素替換為38后,拷貝到鏈表llist < 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; } // 在序列范圍內,均填充上某新值。// 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 0fill (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。三個參數:起始位置、幾個、新值。// 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 10fill_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個元素填充為2fill_n(v.begin(), 5, 2);for_each(v.begin(), v.end(), print);cout << endl;//全部填充為3fill_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(); } // 在指定范圍內,填充上第三個參數gen產生的值。 // gen是個函數(對象),木有參數。所以產生的值,純粹取決于gen,與容器原來的元素無關// generate algorithm example #include <iostream> #include <algorithm> #include <vector> #include <ctime> #include <cstdlib> using namespace std;// function generator: int RandomNumber () { return (rand()%100); } // 產生一百以內的隨機數// class generator: struct c_unique {int current;c_unique() {current=0;} // 構造函數,設成員current初始值為0int operator()() {return ++current;} // 調用操作符()重載,使此類能產生函數對象 } 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; //等差數列an+1=an + 3 class sequence {public:int a;sequence(){ a = 0; } // 構造函數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()產生的值。三個參數:起始位置、幾個、gen產生的值。// 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); // 因為產生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個偽隨機數generate_n(v.begin(), 3, rand);for(unsigned int i = 0; i < v.size(); i++)cout << v[i] << ' ';cout << endl;return 0; }

?


?? 22.15 移除復制remove_copy
remove_copy? Copy range removing value (function template)

// 22.15 移除復制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; } // 將容器中,不等于某值的元素,復制到另一容器中。 // 參數有四個:前兩個表示范圍,第三個是另一容器的起始位置,第四個是某值。 // 這樣就產生了一個問題,另一容器,可以是同一容器么?可以的,請看下面第二個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 20vector<int> myvector (8); // 如改成2,可以看到,容器不會自動擴充,只會復制進2個數。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 20vector<int> myvector (myints,myints+8);vector<int>::iterator it;// 10 30 30 10 10 [10 10 20remove_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 條件移除復制remove_copy_if
remove_copy_if? Copy range removing values (function template)

// 22.16 條件移除復制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 的帶謂詞判斷的版本。把第四個參數,改成了函數(對象)。// 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); // 是奇數的,被刪除 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) {//偶數return x % 2 ? 0 : 1; } int main(void) {//初始化向量vvector < int > v;v.push_back(7);v.push_back(2);v.push_back(5);v.push_back(4);v.push_back(1);//初始化數組iArrayint iArray[6] = {0};//移除v中偶數,剩余元素復制到iArray remove_copy_if(v.begin(), v.end(), iArray, even);//打印v,v沒有改變 for_each(v.begin(), v.end(), print);cout << endl;//打印iArrayfor_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; } // 將容器中等于某值的元素刪除。 // 因為在同一容器中操作,所以返回值值得關注。返回排列完刪除某些元素之后的序列的后一個位置。 // 如果要將數據復制到另一容器,可以考慮用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) {//初始化向量vvector < int > v;v.push_back(2);v.push_back(4);v.push_back(3);v.push_back(4);v.push_back(8);//移除4vector < 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的第三個參數,改成了謂詞判斷。// 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) {//偶數return x % 2 ? 0 : 1; } int main(void) {//初始化向量vvector < int > v;v.push_back(7);v.push_back(2);v.push_back(5);v.push_back(4);v.push_back(1);//移除偶數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 不連續重復元素復制unique_copy
unique_copy? Copy range removing duplicates (function template)

// 22.19 不連續重復元素復制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左參數,是左邊那個與當前值不同那個元素,右參數是當前元素 }return ++result; // 左閉右開原則,返回時要+1 } // 將連續相等的元素過濾掉,只剩下一個 // 第四個參數可選,如有,是謂詞判斷的函數(對象)。這個謂詞估計不常用。 // 第三個參數是輸出的第一個位置。可以用同一容器,也可以用不同容器。// 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 0vector<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 0for_each(iArray, iArray + 6, print);cout << endl;return 0; }

?


?? 22.20 剔除連續重復元素unique
unique? Remove consecutive duplicates in range (function template)

// 22.20 剔除連續重復元素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; } // 將連續相等的元素過濾掉,只剩下一個。在同一容器操作。 // 第三個參數可無,如有是謂詞判斷函數(對象)// 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 10vector<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); } // 反轉元素// 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 反向復制reverse_copy
reverse_copy? Copy range reversed (function template)

// 22.22 反向復制reverse_copy --------------------------------------------------- template <class BidirectionalIterator, class OutputIterator>OutputIterator reverse_copy ( BidirectionalIterator first,BidirectionalIterator last, OutputIterator result ) {while (first!=last) *result++ = *--last;return result; }// 從后向前,復制到另一容器。 // 復制到同一容器,可以嗎? 請見以下第二個程序,my test。 // my test:復制到同一空間(范圍),會產生沖突。復制到同一容器不同范圍,木有關系。// 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:復制到同一空間(范圍),會產生沖突。復制到同一容器不同范圍,木有關系。 #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 旋轉rotate
rotate? Rotate elements in range (function template)

// 22.23 旋轉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; // 這種交換方法,非常新穎。可以畫圖理解 } } // 旋轉交換。[first,middle) 與 [middle,last),兩區域的元素,進行交換。兩區域的元素個數不一定要相等。 // 如 1,2,3,4,5 ,以4為middle,--> 4,5,1,2,3 。 三個參數: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 << "開始旋轉,middle元素=" << *(v.begin() + 7) << endl;rotate(v.begin(), v.begin() + 7, v.end());for_each(v.begin(), v.end(), print);return 0; }

?


?? 22.24 旋轉復制rotate_copy
rotate_copy? Copy rotated range (function template)

// 22.24 旋轉復制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類似,無非是拷貝到了另一容器。四個參數: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 << "開始旋轉復制,*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 洗牌。第三個參數可無,如有,是自定義的隨機發生器。// 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不是標準函數。我們可以采用另兩個函數,來達到相同的目的: #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; //采樣個數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; } // 把序列分成兩部分,前一部分滿足條件,后一部分不滿足條件。三個參數。// 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 容器穩定分割stable_partition
stable_partition? Divide range in two groups - stable ordering (function template)??

// 22.28 容器穩定分割stable_partition ---------------------------------------------------// partition 的穩定版本// 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;//進行穩定分割int *result = stable_partition(iArray, iArray + 10, less10);cout << "按小于10穩定分割,iArray數組變為" << endl;for_each(iArray, iArray + 10, print);cout << endl;// cout << "iArray數組有如下小于10的整數" << endl;for_each(iArray, result, print);cout << endl;// cout << "stable_partition算法返回的分界元素為 " << *result << endl;return 0; }

?


?? 22.29 本章小結

?

/***************************************************************************************************************// 22.29 本章小結 ---------------------------------------------------22.1 元素復制copy // 把序列一中某范圍內的元素,復制到序列二中去。前兩個參數是序列一范圍,第三個參數是序列二的開始位置 // 當序列二不夠大時,不夠裝部分將復制不過去。22.2 反向復制copy_backward // 把序列一中某范圍內的元素,復制到序列二中去。前兩個參數是序列一范圍,第三個參數是序列二的結束位置的下一個位置。 // 兩序列可以是同一序列22.3 元素交換swap // 不通過指針交換兩個元素22.4 迭代器交換iter_swap // 利用指針(迭代器)交換值22.5 區間元素交換swap_ranges // 兩個區間中的元素分別交換。只有三個參數,第四個參數又省了 // 注意:第二個序列要足夠長,否則會出現異常。22.6 元素變換transform // 加轉換的copy。而且轉換函數可以是一元的,也可以是二元的。 // 一元的參數有四個:開始兩個表示第一序列范圍,第三個是結果存放開始位置,第四個是一元函數(對象) // 二元的參數有五個:開始兩個表示第一序列范圍,第三個是第二序列開始位置,第四個是結果存放開始位置,第五個是二元函數(對象)22.7 替換Replace // 在指定范圍內舊值換新值22.8 條件替換replace_if // 當元素滿足什么條件時,更新為新值22.9 替換和復制replace_copy // 將第一序列某范圍內的元素,復制到第二序列中去。在這個復制過程中,將舊值,替換為新值 // 前兩個參數表示第一序列范圍,第三個參數為第二序列開始位置(木有結束位置),第四、五個參數為舊、新值22.10 條件替換和復制replace_copy_if // replace_copy的謂詞判斷版本。 木有舊值了,替換為一個謂詞。這樣應用范圍更廣22.11 填充fill // 在序列范圍內,均填充上某新值。22.12 n次填充fill_n // 在起始位置開始,填充n個value。三個參數:起始位置、幾個、新值。22.13 隨機生成元素generate // 在指定范圍內,填充上第三個參數gen產生的值。 // gen是個函數(對象),木有參數。所以產生的值,純粹取決于gen,與容器原來的元素無關22.14 隨機生成n個元素generate_n // 在開始位置之后,填充n個gen()產生的值。三個參數:起始位置、幾個、gen產生的值。22.15 移除復制remove_copy // 將容器中,不等于某值的元素,復制到另一容器中。 // 參數有四個:前兩個表示范圍,第三個是另一容器的起始位置,第四個是某值。 // 這樣就產生了一個問題,另一容器,可以是同一容器么?可以的。22.16 條件移除復制remove_copy_if // remove_copy 的帶謂詞判斷的版本。把第四個參數,改成了函數(對象)。22.17 移除remove // 將容器中等于某值的元素刪除。 // 因為在同一容器中操作,所以返回值值得關注。返回排列完刪除某些元素之后的序列的后一個位置。 // 如果要將數據復制到另一容器,可以考慮用remove_copy22.18 條件移除remove_if // remove的謂詞判斷版本。 remove的第三個參數,改成了謂詞判斷。22.19 不連續重復元素復制unique_copy // 將連續相等的元素過濾掉,只剩下一個 // 第四個參數可選,如有,是謂詞判斷的函數(對象)。這個謂詞估計不常用。 // 第三個參數是輸出的第一個位置。可以用同一容器,也可以用不同容器。22.20 剔除連續重復元素unique // 將連續相等的元素過濾掉,只剩下一個。在同一容器操作。 // 第三個參數可無,如有是謂詞判斷函數(對象)22.21 元素反向reverse // 反轉元素22.22 反向復制reverse_copy // 從后向前,復制到另一容器。 // 復制到同一容器,可以嗎? 請見以下第二個程序,my test。 // my test:復制到同一空間(范圍),會產生沖突。復制到同一容器不同范圍,木有關系。22.23 旋轉rotate // 旋轉交換。[first,middle) 與 [middle,last),兩區域的元素,進行交換。兩區域的元素個數不一定要相等。 // 如 1,2,3,4,5 ,以4為middle,--> 4,5,1,2,3 。 三個參數:first, middle, last22.24 旋轉復制rotate_copy // 與rotate類似,無非是拷貝到了另一容器。四個參數:first, middle, last, result22.25 隨機抖動random_shuffle // shuffle 洗牌。第三個參數可無,如有,是自定義的隨機發生器。22.26 隨機采樣random_sample //random_sample不是標準函數。我們可以采用另兩個函數,來達到相同的目的//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 // 把序列分成兩部分,前一部分滿足條件,后一部分不滿足條件。三個參數。22.28 容器穩定分割stable_partition // partition 的穩定版本22.29 本章小結 // 這就是小結***********************************************************************************************************/

?

?

?

?

?

??

?

?

00TOP00

?

轉載于:https://www.cnblogs.com/xin-le/p/4115586.html

總結

以上是生活随笔為你收集整理的第22章 变易算法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 青青草视频观看 | 91chinese在线| 欧美日韩一卡二卡 | 国产精品人人做人人爽 | 亚洲无色 | 插插操操 | 国产精品成人免费一区二区视频 | 欧美成人黑人猛交 | 中国肥胖女人真人毛片 | 色之久久综合 | 大学生av | 国产欧美日韩精品一区二区三区 | 自拍偷拍亚洲精品 | 人妻 日韩 欧美 综合 制服 | 亚洲色偷精品一区二区三区 | 日韩国产欧美在线视频 | 国产亚洲精品久久久 | 中国美女一级黄色片 | 奇米四色在线观看 | 粗喘呻吟撞击猛烈疯狂 | 91射| 日本黄色高清视频 | 欧美日韩一区二区三区在线播放 | 美国三级视频 | www.国产免费| 在线免费黄色片 | 伊人艹| 欧美性极品少妇xxxx | av影库 | 国产5区 | av在线中文| 99国产精品欲 | 美女张开腿让男人操 | 国产毛片高清 | 免费看a毛片 | 欧美色图第一页 | 诱惑の诱惑筱田优在线播放 | julia一区二区中文久久97 | 国产大片一区二区三区 | 久久亚洲av午夜福利精品一区 | 久久国产精品免费观看 | 中文字幕亚洲乱码熟女一区二区 | 日本一级理论片在线大全 | 国产一区在线视频观看 | 天天搞夜夜 | 亚洲视频天堂 | 影音先锋中文字幕人妻 | 天天干天天色综合 | 日本一级理论片在线大全 | 上原亚衣av一区二区三区 | 亚洲国产三级 | 99久久久无码国产精品6 | 日本不卡网站 | 国产精品三级在线观看 | 谁有av网址| 三级av在线播放 | 69xx视频在线观看 | 无码人妻一区二区三区一 | aaaa视频| 成人做爰69片免费看 | 日韩女优在线观看 | 成人综合网址 | 欧美性生活视频 | 污污视频在线观看网站 | 欧美中出 | 领导揉我胸亲奶揉下面 | 九九热综合 | 久久精品久久久精品美女 | 日韩在线第三页 | 久久精品日韩 | 青青操视频在线观看 | 一级高清视频 | 国产一区二区三区四 | 久久人人爽人人爽人人av | 韩国av网| 亚洲性色av| 最新毛片基地 | 国产一级一区 | 亚洲乱熟女一区二区三区小说 | 色哟哟在线观看 | 无码人妻久久一区二区三区 | 日本三级一区二区 | 久久精品www | 色屁屁一区二区三区视频 | 97在线公开视频 | 男女一区二区三区 | 肉体粗喘娇吟国产91 | 欧美在线一区二区三区四区 | 日韩欧美精品在线视频 | 中文字幕精品一区二区精品 | 先锋影音av在线 | xx久久 | 日本少妇全体裸体洗澡 | 亚洲精品国产视频 | 狠狠干一区二区 | 日本一区二区高清免费 | 欧美成人精品欧美一级乱黄 | 国产日韩成人内射视频 | 国产三级按摩推拿按摩 |