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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++ Primer 5th笔记(10)chapter10 泛型算法 :write

發布時間:2025/3/21 c/c++ 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++ Primer 5th笔记(10)chapter10 泛型算法 :write 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1. fill: 對給定區間全部賦予某值(algorithm.h)

將指定范圍內的每個元素都設定為給定的值。如果輸入范圍有效,則可以安全寫入。這個算法只會對輸入范圍內已存在的元素進行寫入操作。

template<class FwdIt, class T>
void fill(FwdIt first, FwdIt last, const T& x);

fill(vec.begin(),vec.end(),0);//將每個元素都重置為0 fill(v.begin(), v.begin() + v.size()/2, 10); //將容器的一個子序列設置為0

eg. “0 0 0 0 0 0 0”

vector<int> v2 = { 4,5,6,7,8,9,4 };fill(v2.begin(), v2.end(), 0);//將每個元素都重置為0for(auto it:v2)cout << " " << it;

2. fill_n

對給定迭代器后的n個元素賦予某值。(從迭代器指向的元素開始,將指定數量的元素設置為給定的值)

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);//調用錯誤,fill_n并不是向容器中插入元素,它只負責更新元素的值。

eg. “0 0 0 7 8 9 4”

vector<int> v2 = { 4,5,6,7,8,9,4 };fill_n(v2.begin(), 3, 0);//將每個元素都重置為0for (auto it : v2)cout << " " << it;

3. back_inserter:

back_inserter 實參是一個容器的引用,返回一個綁定在該容器上的插入迭代器。

  • 需要確保算法有足夠的元素存儲輸出數據 。
  • 當對此迭代器賦值時,就push_back一個元素

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);//將每個元素都重置為0for (auto it : v2)cout << " " << it;

4. copy

向目的位置迭代器指向的輸出序列中的元素寫入數據,接受三個迭代器(前兩個指定輸入范圍,第三個指向目標序列的第一個元素。長度同樣需要由程序員來保證

template<class InIt, class OutIt>
OutIt copy(InIt first, InIt last, OutIt x);

copy算法帶有三個迭代器參數:
eg. “0 1 2 3 4 5 6”

vector<int> v2 = { 4,5,6,7,8,9,4 }; int a1[] = { 0,1,2,3,4,5,6 }; auto ret = copy(begin(a1), end(a1), v2.begin());//把a1的內容拷貝給v2 for (auto it : v2)cout << " " << it;

5. replace

讀入一個序列范圍,將序列中某個值全部用一個新值來替換。
template<caass FwdIt, class T>
void replace(FwdIt first, FwdIt last, const T& vold, const T& vnew);
//該算法指定范圍[first, last)內的所有元素值為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

保留原序列不變,此算法額外接受第三個迭代器參數,指出調整后序列的保存位置。指定保存替換后的序列的目標位置, 替換后放在另外一個序列中

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排序算法是穩定排序。(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

vector<string> v2 = { "2222222222222","3" , "winter1", "apples1" , "apples" , "winter"};vector<string> v1; v1.assign(v2.begin(), v2.end()); std::cout << "using stable_sort:";std::stable_sort(v1.begin(), v1.end(), less_len);for (auto it : v1)cout << " " << it;std::cout << '\n';v1.assign(v2.begin(), v2.end());std::cout << "using sort:";std::sort(v1.begin(), v1.end(), less_len);for (auto it : v1)cout << " " << it;std::cout << '\n';

8.unique和unique_copy

  • unique函數執行重排的操作,并不包含“去”的過程(即不會刪除元素,算法不會改變容器大小)只是在有重復元素時,把后面的元素向前移動覆蓋了原來的元素。函數返回的迭代器指向無重復元素序列最后一個元素的下一個位置。

  • unique_copy是它的“_copy”版本,返回的是生成的序列的最后一個元素的下一個位置。(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調用后,原序列的前面部分是無重復元素的序列,而后半部分是剩下沒有被覆蓋的序列。這里,需要手動刪除后面的元素序列,范圍由返回的迭代器和容器末端決定。

  • 刪除一個空范圍沒有影響。
    eg. “fox quick red slow the”
vector<string> words = { "the", "quick", "red", "fox","red", "the","slow" };sort(words.begin(), words.end());auto end_unique = unique(words.begin(), words.end());cout << " " << *end_unique;//值不確定words.erase(end_unique, words.end());for (auto it : words)cout << " " << it;std::cout << '\n';

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';

【引用】

  • 代碼 https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/genericAlgorithm.h
  • 總結

    以上是生活随笔為你收集整理的C++ Primer 5th笔记(10)chapter10 泛型算法 :write的全部內容,希望文章能夠幫你解決所遇到的問題。

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