C++ 常用查找算法

#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <algorithm>
using namespace std;
#include <vector>
#include <string>
#include <functional>
/*
find算法 查找元素
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param value 查找的元素
@return 返回查找元素的位置
*/
void test01()
{vector<int>v;for (int i = 0; i < 10;i++){v.push_back(i);}vector<int>::iterator pos = find(v.begin(), v.end(), 5);if (pos!=v.end()){cout << "找到了數據:" << *pos << endl;}else{cout << "未找到" << endl;}}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;
};
//利用find查找自定義數據類型
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;}
}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", 20);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;}
}/*
adjacent_find算法 查找相鄰重復元素
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param  _callback 回調函數或者謂詞(返回bool類型的函數對象)
@return 返回相鄰元素的第一個位置的迭代器
*/
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算法 二分查找法
注意: 在無序序列中不可用
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param value 查找的元素
@return bool 查找返回true 否則false
*/
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 << "找到了4" << endl;}else{cout << "未找到" << endl;}}/*
/*
count算法 統計元素出現次數
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param  value回調函數或者謂詞(返回bool類型的函數對象)
@return int返回元素個數
*//*
count_if算法 統計元素出現次數
@param beg 容器開始迭代器
@param end 容器結束迭代器
@param  callback 回調函數或者謂詞(返回bool類型的函數對象)
@return int返回元素個數*/
class GreaterThenFour
{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(), GreaterThenFour());cout << "大于等于 4的個數為" << num << endl;}int main(){//test01();//test02();//test03();//test04();//test05();test06();system("pause");return EXIT_SUCCESS;
}

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

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

相關文章

CentOS7卸載并安裝mysql教程

MySQL安裝 先卸載其他 刪除Mysql yum remove mysql mysql-server mysql-libs mysql-server;find / -name mysql 將找到的相關東西delete掉(rm -rf /var/lib/mysql)&#xff1b;rpm -qa|grep mysql(查詢出來的東東yum remove掉) rm /etc/my.cnf查看是否還有mysql軟件&#x…

C++ 常用排序算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> using namespace std; #include <algorithm> #include <vector> #include <functional> #include <ctime> /* merge算法 容器元素合并&#xff0c;并存儲到另一容器中 這兩個容器 必須也是…

排序穩定性的意義

首先&#xff0c;為什么會有排序算法穩定性的說法&#xff1f;只要能排好不就可以了嗎&#xff1f; 看例子 第1行是數字2 記作 1 2 第2行是數字4 記作 2 4 第3行是數字2 記作 3 2 排序后的結果&#xff08;如果看不懂命令的意思&#xff0c;參照這個博客&#xff09; 那么引入…

C++ 常用拷貝和替換算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> #include <algorithm> #include <iterator> using namespace std;/* copy算法 將容器內指定范圍的元素拷貝到另一容器中 param beg 容器開始迭代器 param end 容器結束迭代器 p…

防火墻的基礎知識入門

文章目錄防火墻基于實現方式&#xff0c;防火墻的發展分為四個階段:Linux 環境中主要的防火墻形式TCP wrappers~~詳解~~ 粗解Tcp wrappers的認識它的基本過程是這樣的&#xff1a;iptable攻擊和防御DDOS 攻擊常見的可能受到 DDOS 攻擊體現的癥狀有&#xff1a;而常見的 DDOS 攻…

C++ 常用算數生成算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <vector> using namespace std; #include <algorithm> //不好使 #include <numeric> //好使 #include <iterator> /* accumulate算法 計算容器元素累計總和 param beg 容器開始迭代…

fork()請問下面的程序一共輸出多少個“A”?多少個-?

題目&#xff1a;請問下面的程序一共輸出多少個“-”&#xff1f; #include #include #include int main(void) { int i; for(i0; i<2; i){ fork(); printf("-"); } return 0; } 解析&#xff1a;一共輸出8個。 首先程序一開始&am…

C++ 常用集合算法

#define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <algorithm> #include <vector> #include <iterator> using namespace std;/* set_intersection算法 求兩個set集合的交集 注意:兩個集合必須是有序序列 param beg1 容器1開始迭代器 par…

本能富可敵國,最后卻選擇拯救世界!Bram的Vim和烏干達兒童

他本能富可敵國&#xff0c;最后卻選擇拯救世界 在命令行界面輸入vim會出現一堆文件&#xff0c;但是一直有這么一句話 Help poor children in Uganda! “幫助可憐的烏干達兒童” 查詢了一下這里面相關的歷史背景和知識 在Vim許可證文件結束后的部分翻譯 &#xff0d;如果…

linux 常用命令01

/bin/bash 就是linux默認的shell ls命令 ls -a 顯示所有文件 包含隱藏文件 ls -R 遞歸顯示子目錄 ls -l 顯示詳細信息 ls -lrt 按照時間排序&#xff0c;顯示文件信息 配合通配符使用 ls *.c *匹配任意多個字符 ls xx.? 匹配任意一個字符 cd 命令 cd - 為切換到上次目錄 cd 回…

Linux基礎查漏補缺

文章目錄第二遍重新回顧Linux基礎查看主機名修改主機名查看IP地址Linux的 “--”和“-”根目錄文件的意義和作用alias直接在命令行界面輸入firefox數組越界發生什么命令行光標移動的幾個操作重定向第二遍重新回顧Linux基礎 1.查找忽略的知識點 2.再次記憶一些基礎知識 3.鞏固基…

linux 常用命令02--文件屬性 以及軟硬鏈接

文件屬性和用戶用戶組 通過ls-l 顯示文件詳細信息 drwxrwxr-x 2 user usergroup 4096 10月 30 20:55 stu1drwxrwxr-x d代表目錄文件&#xff0c; -代表普通文件 rwx rwx r-x 歸屬用戶的權限 歸屬組的權限 其他用戶的權限 權限位數字表示法(8進制數…

linux查漏補缺之常用命令

wc命令 -c, --bytes, --chars輸出字節統計數。-l, --lines輸出換行符統計數。-L, --max-line-length輸出最長的行的長度。-w, --words輸出單詞統計數。grep命令 圖解

linux 常用命令03--修改文件的權限與歸屬

chmod 命令 改變文件權限 第一種&#xff1a; chmod [u|g|o|a] [|-] [r|w|x] filename 比如&#xff1a; chmod ux filename 給所屬用戶增加執行的權限第二種&#xff1a; 給a.out 文件&#xff0c;所屬用戶可讀可寫&#xff0c;所屬組可讀可寫&#xff0c;其他的讀 chmod 06…

思維導圖:面試小結

文件&#xff1a;思維導圖

linux 常用命令04 查找和檢索

先說一下 文件的基本類型 文件類型 l 符號鏈接文件&#xff08;軟連接&#xff09; b 塊設備 &#xff08;磁盤文件&#xff09;c 字符設備p 管道設備&#xff08;pipe&#xff09;s 本地套接字&#xff08;網絡編程&#xff09;- 普通文件 用find命令的時候&…

linux 常用命令05 常用的壓縮與解壓縮文件

zip/unzip ----zip格式 使用方式&#xff1a;zip -r 壓縮包名 原材料 -r代表遞歸子目錄 原材料可以有多個 例如&#xff1a;zip -r bb.zip bb hello 對應的解壓縮&#xff1a;unzip bb.zip .gz格式的壓縮包 gzip和gunzip tar 最常用打包工具 .tar.gz tar相應參數介紹 -c 壓縮…

apt-howto

https://www.debian.org/doc/manuals/apt-howto/index.zh-cn.html#contents

Linux系統監控shell腳本

開源項目 https://github.com/atarallo/TECMINT_MONITOR #! /bin/bash # unset any variable which system may be usingunset tecreset os architecture kernelrelease internalip externalip nameserver loadaveragewhile getopts iv name docase $name ini)iopt1;;v)vopt1…

linux ubuntu 軟件安裝的三種方式

apt-get 自動安裝軟件&#xff0c;解決依賴關系 sudo apt-get update 更新源 源在 /etc/apt/sources.list 文件中sudo apt-get install softwarename sudo apt-get remove softwarenamedpkg 根據deb安裝包來安裝軟件 dpkg 是“Debian Packager ”的簡寫 sudo dpkg -i xxx.de…