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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

c++中STL的常用算法---2(遍历算法,查找算法)

發布時間:2023/11/30 c/c++ 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++中STL的常用算法---2(遍历算法,查找算法) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

算法概述

  • 算法主要由頭文件組成
  • 是所有STL頭文件中最大的一個,其中常用的功能涉及到比較,交換,查找,遍歷,復制,修改,反轉,排序,合并等
  • 體積很小,只包括在幾個序列容器上進行的簡單運算的模板函數
  • 定義一些模板類,用以聲明函數對象
  • 遍歷算法

    for_each()

  • for_each: 用指定函數依次對指定范圍內所有元素進行迭代訪問。該函數不得修改 序列中的元素。
  • for_each基礎遍歷

    #include<iostream>using namespace std; #include<vector> #include<algorithm> #include<functional> //回調函數 //void myPrint(int v) //{ // cout << v << endl; //}//仿函數 struct myPrint01 { public:void operator()(int v){cout << v << endl;} };void test01() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), myPrint01());}

    for_each可以保存內部記錄,有返回值

    struct myPrint02 { public:void operator()(int v){cout << v << endl;this->m_Count++;}int m_Count; }; void test02() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}myPrint02 print2 = for_each(v.begin(), v.end(), myPrint02());cout << print2.m_Count << endl; }

    for_each可以綁定參數進行輸出

    struct myPrint03:public binary_function <int,int,void> { public:void operator()(int v,int start) const{cout << v+start << endl;} };void test03() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), bind2nd(myPrint03(), 10000)); }

    transform()

  • transform: 與 for_each 類似,遍歷所有元素,但可對容器的元素進行修改
  • 注意,transform 不會給目標容器分配內存,所以需要我們提前分配好內存
  • transform基礎遍歷

    class TransForm { public:int operator()(int val){return val+10;} }; void test04() {vector<int>v; //原容器for (int i = 0; i < 10; i++){v.push_back(i);}vector<int>vTarget; //目標容器vTarget.resize(v.size());//開辟空間transform(v.begin(), v.end(), vTarget.begin(), TransForm());for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << endl; }); }

    transform第二種用法,把兩個容器搬到第三個容器中

    class TransForm2 { public:int operator()(int val,int val2){return val + val2;} };void test05() {vector<int>v1;vector<int>v2;for (int i = 0; i < 10; i++){v1.push_back(100 + i);v2.push_back(200 + i);}vector<int>vTarget;//目標容器vTarget.resize(v1.size());//提供空間transform(v1.begin(), v1.end(), v2.begin(), vTarget.begin(), TransForm2());for_each(vTarget.begin(), vTarget.end(), [](int val){cout << val << endl; }); }

    查找算法

    adjacent_find()

    在 iterator 對標識元素范圍內,查找一對相鄰重復元素,找到則返回指向這對元素的第 一個元素的迭代器。否則返回 past-the-end。

    void test04() {vector<int>v;v.push_back(2);v.push_back(3);v.push_back(4);v.push_back(5);v.push_back(5);v.push_back(6);v.push_back(2);vector<int>::iterator pos=adjacent_find(v.begin(), v.end());if (pos != v.end()){cout << "找到了相鄰元素" << *pos << endl;}else{cout << "未找到" << endl;} }

    binary_search

    在有序序列中查找 value,找到則返回 true。注意:在無序序列中,不可使用。

    void test05() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}bool ret=binary_search(v.begin(), v.end(), 4);if (ret){cout << "找到了" << endl;}else{cout << "未找到" << endl;} }

    count()

    利用等于操作符,把標志范圍內的元素與輸入值比較,返回相等的個數。

    count_if()

    按條件進行比較

    class GreateThenFour { public:bool operator()(int v){return v >=4;} }; void test06() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}v.push_back(4);v.push_back(4);v.push_back(4);v.push_back(4);int num=count(v.begin(), v.end(), 4);cout << "4的個數為:" << num << endl;num =count_if(v.begin(), v.end(), GreateThenFour());cout << "大于等于4的個數為:" << num << endl; }

    find()

  • find: 利用底層元素的等于操作符,對指定范圍內的元素與輸入值進行比較。當匹 配時,結束搜索,返回該元素的迭代器。

  • equal_range: 返 回 一 對 iterator , 第 一 個 表 示 lower_bound, 第 二 個 表 示 upper_bound。

    void test01() {vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}find(v.begin(), v.end(), 5);//查找這個容器中有沒有5vector<int>::iterator pos = find(v.begin(), v.end(), 5);if (pos != v.end()){cout << "找到了數據:" << *pos << endl;}else{cout << "沒找到" << endl;} }
  • 利用find查找自定義數據類型

    class Person { public:Person(string name, int age){this->m_Name = name;this->m_Age = age;}bool operator==(const Person&p){if (this->m_Name ==p.m_Name&&this->m_Age == p.m_Age){return true;}return false;}string m_Name;int m_Age; }; void test02() {vector<Person>v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);vector<Person>::iterator pos= find(v.begin(), v.end(), p2);if (pos != v.end()){cout << "找到了數據姓名:" << (*pos).m_Name<<"年齡:"<<pos->m_Age << endl;}else{cout << "沒找到" << endl;} }

    find_if()

    find_if: 使用輸入的函數代替等于操作符執行 find。返回被找到的元素的迭代器。 假設 vectorvecIntA,vecIntA 包含 1,3,5,3,9 元素

    //find_if class MyCompare:public binary_function<Person*,Person*,bool> { public:bool operator ()(Person *p1,Person *p2)const{if (p1->m_Name==p2->m_Name&&p1->m_Age==p2->m_Age){return true;}return false;} }; void test03() {vector<Person *>v;Person p1("aaa", 10);Person p2("bbb", 20);Person p3("ccc", 30);Person p4("ddd", 40);v.push_back(&p1);v.push_back(&p2);v.push_back(&p3);v.push_back(&p4);Person *p = new Person("bbb", 21);vector<Person *>::iterator pos= find_if(v.begin(), v.end(),bind2nd(MyCompare(),p));if (pos != v.end()){cout << "找到了數據姓名:" << (*pos)->m_Name << "年齡:" << (*pos)->m_Age << endl;}else{cout << "沒找到" << endl;} }

    總結

    以上是生活随笔為你收集整理的c++中STL的常用算法---2(遍历算法,查找算法)的全部內容,希望文章能夠幫你解決所遇到的問題。

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