C++ Primer 5th笔记(10)chapter10 泛型算法 :write
1. fill: 對(duì)給定區(qū)間全部賦予某值(algorithm.h)
將指定范圍內(nèi)的每個(gè)元素都設(shè)定為給定的值。如果輸入范圍有效,則可以安全寫(xiě)入。這個(gè)算法只會(huì)對(duì)輸入范圍內(nèi)已存在的元素進(jìn)行寫(xiě)入操作。
template<class FwdIt, class T>
void fill(FwdIt first, FwdIt last, const T& x);
eg. “0 0 0 0 0 0 0”
vector<int> v2 = { 4,5,6,7,8,9,4 };fill(v2.begin(), v2.end(), 0);//將每個(gè)元素都重置為0for(auto it:v2)cout << " " << it;2. fill_n
對(duì)給定迭代器后的n個(gè)元素賦予某值。(從迭代器指向的元素開(kāi)始,將指定數(shù)量的元素設(shè)置為給定的值)
template<class OutIt, class Size, class T>
void fill_n(OutIt first, Size n, const T& x);
eg.
fill_n(vec.begin(),n,val); vector<int> vec; fill_n(vec.begin(), 10, 0);//調(diào)用錯(cuò)誤,fill_n并不是向容器中插入元素,它只負(fù)責(zé)更新元素的值。eg. “0 0 0 7 8 9 4”
vector<int> v2 = { 4,5,6,7,8,9,4 };fill_n(v2.begin(), 3, 0);//將每個(gè)元素都重置為0for (auto it : v2)cout << " " << it;3. back_inserter:
back_inserter 實(shí)參是一個(gè)容器的引用,返回一個(gè)綁定在該容器上的插入迭代器。
- 需要確保算法有足夠的元素存儲(chǔ)輸出數(shù)據(jù) 。
- 當(dāng)對(duì)此迭代器賦值時(shí),就push_back一個(gè)元素
eg. “4 5 6 7 8 9 4 0 0 0”
vector<int> v2 = { 4,5,6,7,8,9,4 }; fill_n(back_inserter(v2), 3, 0);//將每個(gè)元素都重置為0for (auto it : v2)cout << " " << it;4. copy
向目的位置迭代器指向的輸出序列中的元素寫(xiě)入數(shù)據(jù),接受三個(gè)迭代器(前兩個(gè)指定輸入范圍,第三個(gè)指向目標(biāo)序列的第一個(gè)元素。長(zhǎng)度同樣需要由程序員來(lái)保證)
template<class InIt, class OutIt>
OutIt copy(InIt first, InIt last, OutIt x);
copy算法帶有三個(gè)迭代器參數(shù):
eg. “0 1 2 3 4 5 6”
5. replace
讀入一個(gè)序列范圍,將序列中某個(gè)值全部用一個(gè)新值來(lái)替換。
template<caass FwdIt, class T>
void replace(FwdIt first, FwdIt last, const T& vold, const T& vnew);
//該算法指定范圍[first, last)內(nèi)的所有元素值為vold替換為vnew。
eg. “42 5 6 7 8 9 42”
vector<int> v2 = { 4,5,6,7,8,9,4 };int a1[] = { 0,1,2,3,4,5,6 }; replace(v2.begin(), v2.end(), 4, 42); for (auto it : v2)cout << " " << it;6.replace_copy
保留原序列不變,此算法額外接受第三個(gè)迭代器參數(shù),指出調(diào)整后序列的保存位置。指定保存替換后的序列的目標(biāo)位置, 替換后放在另外一個(gè)序列中
template<class InIt, class OutIt, class T>
OutIt replace_copy(InIt first, InIt last, OutIt x, const T& vold, const T& vnew);
eg. “42 5 6 7 8 9 42”
vector<int> v2 = { 4,5,6,7,8,9,4 };int a1[] = { 0,1,2,3,4,5,6 };vector<int> v3;replace_copy(v2.cbegin(), v2.cend(), back_inserter(v3), 4, 42);for (auto it : v3)cout << " " << it;7.sort重排元素
stable_sort排序算法是穩(wěn)定排序。(algorithm.h)
template
void sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void sort(RanIt first, RanIt last, Pred pr);
template
void stable_sort(RanIt first, RanIt last);
template<class RanIt, class Pred>
void stable_sort(RanIt first, RanIt last, Pred pr);
eg.
using stable_sort: 3 apples winter winter1 apples1 2222222222222
using sort: 3 apples winter winter1 apples1 2222222222222
8.unique和unique_copy
-
unique函數(shù)執(zhí)行重排的操作,并不包含“去”的過(guò)程(即不會(huì)刪除元素,算法不會(huì)改變?nèi)萜鞔笮?#xff09;只是在有重復(fù)元素時(shí),把后面的元素向前移動(dòng)覆蓋了原來(lái)的元素。函數(shù)返回的迭代器指向無(wú)重復(fù)元素序列最后一個(gè)元素的下一個(gè)位置。
-
unique_copy是它的“_copy”版本,返回的是生成的序列的最后一個(gè)元素的下一個(gè)位置。(algorithm.h)
template
FwdIt unique(FwdIt first, FwdIt last);
template<class FwdIt, class Pred>
FwdIt unique(FwdIt first, FwdIt last, Pred pr);template<class InIt, class OutIt>
OutIt unique_copy(InIt first, InIt last, OutIt x);
template<class InIt, class OutIt, class Pred>
OutIt unique_copy(InIt first, InIt last, OutIt x, Pred pr);
注意:unique調(diào)用后,原序列的前面部分是無(wú)重復(fù)元素的序列,而后半部分是剩下沒(méi)有被覆蓋的序列。這里,需要手動(dòng)刪除后面的元素序列,范圍由返回的迭代器和容器末端決定。
- 刪除一個(gè)空范圍沒(méi)有影響。
eg. “fox quick red slow the”
eg. “1 2 the quick red fox red the slow”
vector<string> wordsNew = { "1", "2" }; vector<string> words = { "the", "quick", "red", "fox","red", "the","slow" }; //Create an insert_iterator for results insert_iterator<vector<string> > ins(wordsNew, wordsNew.end());auto end_unique = unique_copy(words.begin(), words.end(), ins); //cout << " " << end_unique; for (auto it : wordsNew)cout << " " << it; std::cout << '\n';【引用】
總結(jié)
以上是生活随笔為你收集整理的C++ Primer 5th笔记(10)chapter10 泛型算法 :write的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C++ Primer 5th笔记(10)
- 下一篇: C++ Primer 5th笔记(10)