C++STL總結筆記(三)—— 常見算法

文章目錄

  • 一、基本概念
  • 二、程序示例
    • 1.遍歷
    • 2. 查找
    • 3. 排序、拷貝、替換
    • 4. numeric相關算法
  • 總結


一、基本概念

算法是STL中很重要的一部分,其功能包括比較,查找,排序,交換,遍歷,復制等等。

最大的算法頭文件是algorithm,封裝了很多種模板類。還有numeric和functional也比較常見。

二、程序示例

1.遍歷

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;void print(int a)
{cout << a << " ";
}class print1
{
public:void operator()(int a){cout << a << " ";}
};class print2
{
public:int operator()(int a){cout << a << " ";return a;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//函數作為形參進行遍歷輸出for_each(L.begin(), L.end(), print);cout << endl;//仿函數進行遍歷for_each(L.begin(), L.end(), print1());cout << endl;//transform實現遍歷list<int>L1;L1.resize(L.size());transform(L.begin(),L.end(),L1.begin(), print2());}int main()
{test();system("pause");
}

2. 查找

#include<iostream>
#include<list>
#include<algorithm>
using namespace std;class Compare
{
public://一元謂詞bool operator()(int a){return a > 1;}
};class Cat
{
public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定義數據類型需要重載==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};class print1
{
public:bool operator()(Cat& cat){return cat.Age > 3;}
};void test()
{list<int>L;L.push_back(1);L.push_back(2);L.push_back(3);//find算法查找list<int>::iterator i = find(L.begin(), L.end(), 1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i<<endl;}//find_if查找list<int>::iterator i1 = find_if(L.begin(),L.end(),Compare());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << *i1 << endl;}//查找元素是否存在,無序序列結果未知bool i2 = binary_search(L.begin(), L.end(), 1);if (i2){cout << "查找到1" << endl;}else{cout << "未查找到" << endl;}//count統計int n = count(L.begin(), L.end(),1);cout << n << endl;//count_if統計int n1 = count_if(L.begin(), L.end(), Compare());cout << n1 << endl;}void test1()
{list<Cat>L;Cat cat1("小100", 76, 2);Cat cat2("小200", 32, 2);Cat cat3("小300", 32, 4);Cat cat4("小400", 32, 3);Cat cat5("小500", 54, 1);//插入L.push_back(cat1);L.push_back(cat2);L.push_back(cat3);L.push_back(cat4);L.push_back(cat5);//find查找list<Cat>::iterator i = find(L.begin(), L.end(), cat1);if (i == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i).Name << endl;}//find_if查找list<Cat>::iterator i1 = find_if(L.begin(), L.end(), print1());if (i1 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i1).Name << endl;}//查找相鄰的重復元素list<Cat>::iterator i2 = adjacent_find(L.begin(), L.end());if (i2 == L.end()){cout << "未查找到" << endl;}else{cout << "查找到" << (*i2).Name << endl;}Cat cat6("小300", 32, 4);//count統計,需要重載int n = count(L.begin(), L.end(), cat6);cout << n << endl;
}int main()
{test1();system("pause");
}

3. 排序、拷貝、替換

#include<iostream>
#include<vector>
#include<algorithm>
#include<ctime>
using namespace std;class Compare
{
public://一元謂詞bool operator()(int a,int b){return a > b;}
};class Compare3
{
public://一元謂詞bool operator()(int a){return a > 3;}
};void print(int a)
{cout << a << " ";
}class Cat
{
public:Cat(string name, int color, int age){this->Name = name;this->Color = color;this->Age = age;}//自定義數據類型需要重載==bool operator==(const Cat& cat){if (Name == cat.Name && Color == cat.Color && Age == cat.Age){return true;}else{return false;}}public:string Name;int Color;int Age;
};void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//降序sort(L.begin(), L.end(),Compare());for_each(L.begin(), L.end(), print);cout << endl;//greater<int>()sort(L.begin(), L.end(),greater<int>());for_each(L.begin(), L.end(), print);cout << endl;sort(L.begin(), L.end());//隨機打亂//srand((unsigned int)time(NULL));//random_shuffle(L.begin(), L.end());//for_each(L.begin(), L.end(), print);//cout << endl;vector<int>L1(L);vector<int>L2;L2.resize(L.size()+L1.size());//合并,默認只能同為升序的合并merge必須為有序序列merge(L.begin(), L.end(), L1.begin(), L1.end(),L2.begin());for_each(L2.begin(), L2.end(), print);cout << endl;//反轉reverse(L.begin(), L.end());for_each(L.begin(), L.end(), print);cout << endl;//拷貝vector<int>L3;L3.resize(L.size());copy(L.begin(), L.end(), L3.begin());for_each(L3.begin(), L3.end(), print);cout << endl;//替換replace(L.begin(), L.end(), 2, 5);for_each(L.begin(), L.end(), print);cout << endl;replace_if(L.begin(), L.end(), Compare3(),20);for_each(L.begin(), L.end(), print);cout << endl;//互換swap(L, L1);
}int main()
{test();system("pause");
}

4. numeric相關算法

#include<iostream>
#include<vector>
#include<numeric>
#include<algorithm>
#include<ctime>
using namespace std;void print(int a)
{cout << a << " ";
}void test()
{vector<int>L;L.push_back(1);L.push_back(2);L.push_back(3);L.push_back(4);//計算容器元素的和,0為起始累加值int total = accumulate(L.begin(), L.end(), 0);cout << total<<endl;//填充元素vector<int>L1;L1.resize(L.size());fill(L1.begin(), L1.end(), 3);for_each(L1.begin(), L1.end(), print);cout << endl;//求交集vector<int>L2;L2.resize(min(L.size(),L1.size()));vector<int>::iterator i = set_intersection(L.begin(), L.end(), L1.begin(), L1.end(), L2.begin());for_each(L2.begin(), i, print);cout << endl;//求并集vector<int>L3;L3.resize(L.size()+ L1.size());vector<int>::iterator j = set_union(L.begin(), L.end(), L1.begin(), L1.end(), L3.begin());for_each(L3.begin(), j, print);cout << endl;//差集vector<int>L4;L4.resize(max(L.size() , L1.size()));vector<int>::iterator j1 = set_difference(L.begin(), L.end(), L1.begin(), L1.end(), L4.begin());for_each(L4.begin(), j1, print);
}int main()
{test();system("pause");
}

總結

以上只是stl算法中常見的,后續會隨時補充新的算法。

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/256100.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/256100.shtml
英文地址,請注明出處:http://en.pswp.cn/news/256100.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java zip解壓,并遍歷zip中的配置文件 .cfg或.properties

1.解析cfg或properties配置文件 講配置文件&#xff0c;讀取&#xff0c;并封裝成為map類型數據 /*** 解析cfg文件** param cfgFile* return*/public static Map<String, Object> readCfg(FileInputStream cfgFile) {Properties prop new Properties();Map<String, O…

db2 常用配置

db2set配置&#xff1a; db2set DB2_ENABLE_LDAPNO db2set DB2_ALTERNATE_GROUP_LOOKUPGETGROUPLIST db2set DB2_RESTORE_GRANT_ADMIN_AUTHORITIESON db2set DB2_SKIPINSERTEDON db2set DB2_LOAD_COPY_NO_OVERRIDENONRECOVERABLE db2set DB2_EVALUNCOMMITTEDON db2set DB2_SKIP…

安裝完最小化 RHEL/CentOS 7 后需要做的 30 件事情(三)碼農網

12. 安裝 Apache Tomcat Tomcat 是由 Apache 設計的用來運行 Java HTTP web 服務器的 servlet 容器。按照下面的方法安裝 tomcat&#xff0c;但需要指出的是安裝 tomcat 之前必須先安裝 Java。 # yum install tomcat 安裝 Apache Tomcat 安裝完 tomcat 之后&#xff0c;啟動 to…

【圖像處理】——圖像特效處理(馬賽克、圖像融合、毛玻璃等)

參考:https://blog.csdn.net/qq_43328040/article/details/109081414 import cv2 import numpy as np import random#馬賽克:將一定大小窗口的RGB設置成一個顏色 def horseBox(img):row,col,chal = img.shapeboxRow = int(0.3*row)boxcol = int(0.3*col)for m in range(50,b…

JDK5.0新特性之:泛型

文/陳剛 2005-11-09 一、前言 泛型這個詞在現在的JAVA挺時髦&#xff0c;光從字面上你是無法知道它代表些什么東東的&#xff0c;所以我們還是不要從字面去理解&#xff0c;而是從一些實例去了解它吧。 二、泛型之前的日子 &#xff2a;&#xff24;&#xff2b;&#xff11;.…

QT5.14.2基于PCL1.11.1顯示點云(基于Windows VS2019開發環境)

文章目錄一、安裝1.1 PCL安裝1.2 QT安裝1.3 VTK編譯二、程序配置1. 基于mscv創建QT的程序2. 配置QT工程文件和依賴項3. 編寫點云顯示的小程序總結一、安裝 1.1 PCL安裝 PCL1.11.1庫的安裝網上教程很多&#xff0c;推薦一個很好的教程&#xff1a; Win10 系統下 VisualStudio2…

Spring學習筆記—最小化Spring XML配置

自動裝配(autowiring)有助于減少甚至消除配置<property>元素和<constructor-arg>元素&#xff0c;讓Spring自動識別如何裝配Bean的依賴關系。 自動檢測(autodiscovery)比自動裝配更進了一步&#xff0c;讓Spring能夠自動識別哪些類需要被配置成Spring Bean&#xf…

【數據結構】——快速排序

目錄 一、代碼 二、復雜度&#xff1a;O(nlog(n)) 三、快速排序的劣勢 視頻參考鏈接&#xff1a;https://www.bilibili.com/video/BV1mp4y1D7UP?p17 一、代碼 思想&#xff1a;假設是對一個list進行排序 1、選取第一個元素作為p元素&#xff1b; 2、將p元素歸位&#xff0…

讀取數據庫信息構建視圖字段的備注信息,方便程序代碼生成

在很多情況下&#xff0c;我們開發都需要有一個快速的代碼生成工具用來提高開發效率&#xff0c;代碼生成工具很多信息都是讀取數據庫的表、視圖等元數據進行對象表信息的完善&#xff0c;有了這些信息&#xff0c;我們就可以在普通的實體類代碼里面添加屬性字段的中文注釋&…

Ubuntu DNS bind9 配置

下面的配置就是實現解析test.zp.com到不同的IP地址 安裝dns server軟件包$ apt-get install bind9 配置dns配置文件的路徑在/etc/bind路徑下面添加一個zone$ /etc/bind# vim /etc/bind/named.conf.local 添加下面&#xff0c;語法可以參照/etc/bind/zones.rfc1918中的語法添加&…

微博分享錯誤

昨天再做這塊的時候&#xff0c;不知怎么的點擊之后什么反應都沒有&#xff0c;程序也沒有崩&#xff0c;日志倒是輸出了這個錯誤 解決辦法&#xff1a;打開你寫分享的代碼跟API文檔對比一下創建文本、圖片或者網頁的時候是不是少寫了那個屬性&#xff0c;我這里是在創建網頁的…

C++總結筆記(十二)—— 智能指針

文章目錄前言一、智能指針是什么&#xff1f;二、示例總結前言 C對于內存管理的要求很高&#xff0c;如果不及時釋放對象內存&#xff0c;就可能會發生內存泄露或野指針等情況&#xff0c;鑒于這種情況&#xff0c;C11提出了智能指針的概念。 一、智能指針是什么&#xff1f;…

代碼生成工具之界面快速生成

界面開發&#xff0c;無論對于Web開發&#xff0c;還是Winform開發&#xff0c;都需要耗費一定的時間&#xff0c;特別對于一個數據庫字段比較多的界面&#xff0c;一般就需要在編輯界面上擺的更多的控件來做數據顯示&#xff0c;每次碰到這個&#xff0c;都有點頭痛&#xff0…

javascript - 封裝原生js實現ajax

1 /*2 * ajax方法3 */4 var Ajax function() {5 var that this;6 //創建異步請求對象方法7 that.createXHR function() {8 if(window.XMLHttpRequ…

QT對象樹、信號和槽機制

文章目錄一 、對象樹是什么&#xff1f;二、信號和槽的基本概念2.1 信號2.2 槽2.3 松散耦合2.4 特點三、示例總結一 、對象樹是什么&#xff1f; 對象樹是由父類和若干子類對象組成&#xff0c;而子類也可以由若干孫類。 QT中的對象樹是以QObject為起始父類來完成樹的構建的&a…

【數據結構】——歸并排序

目錄 一、代碼 二、隨筆 一、代碼 歸并排序的主要思路&#xff1a;將兩個有序的子列表歸并為一個有序的大列表 #歸并函數&#xff0c;假設li是由左右兩個有序的子列表組成,假設兩個子列表都是從小到大排好序的列表 def merge(li,low,mid,high)::param li: 由左右兩個有序的子列…

開發發布npm module包

開發發布npm module包 問題 在項目開發過程中&#xff0c;每當進入一個新的業務項目&#xff0c;從零開始搭建一套前端項目結構是一件讓人頭疼的事情&#xff0c;就要重新復制一個上一個項目的前端框架和組件代碼庫。其中很多功能的模塊組件都要重復拷貝&#xff0c;可以統一將…

如何使用ATS提高應用的安全性

App Transport Security&#xff0c;簡短的說就是ATS&#xff0c;是iOS9和OS X El Capitan的一個新特性。App Transport Security 的目標是提高Apple 操作系統的安全性以及在此操作系統上運行的任何應用的安全性。 基于HTTP傳輸數據的網絡請求都是明文。開啟App Transport Secu…

手機客戶端測試考慮的點

手機客戶端測試考慮點總結 版權聲明&#xff1a;本文為博主原創文章&#xff0c;未經博主允許不得轉載。 此文未本人工作中的總結&#xff0c;特此總結。 異常場景&#xff1a; 網絡異常&#xff0c;服務器異常&#xff0c;接口異常或參考參數篡改&#xff0c;斷電&#xff0c;…

NMS(非極大值抑制)算法詳解與示例

一、NMS是什么&#xff1f; NMS&#xff08;non maximum suppression&#xff09;即非極大值抑制&#xff0c;廣泛應用于傳統的特征提取和深度學習的目標檢測算法中。 NMS原理是通過篩選出局部極大值得到最優解。 在2維邊緣提取中體現在提取邊緣輪廓后將一些梯度方向變化率較小…