騰云建站靠譜嗎什么是網(wǎng)站
目錄
1.遍歷算法
?1.1for_earch
1.2transform
2.常用查找算法
2.1find,返回值是迭代器
2.1.1查找內(nèi)置數(shù)據(jù)類型?
?2.1.2查找自定義數(shù)據(jù)類型
2.2fin_if 按條件查找元素
2.2.1查找內(nèi)置的數(shù)據(jù)類型
2.2.2查找內(nèi)置數(shù)據(jù)類型
2.3查找相鄰元素adjeacent_find
2.4查找指定元素是否存在binarary_search
2.5統(tǒng)計元素的個數(shù)count
?2.5.1統(tǒng)計內(nèi)置數(shù)據(jù)類型
2.5.2統(tǒng)計自定義數(shù)據(jù)類型
2.6按條件統(tǒng)計元素個數(shù)
2.6.1統(tǒng)計內(nèi)置數(shù)據(jù)類型?
2.6.2統(tǒng)計自定義的數(shù)據(jù)類型
3.常用排序算法
3.1sort
1.遍歷算法
?1.1for_earch
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用遍歷算法 for_each//利用普通函數(shù)實現(xiàn)
void print01(int val)
{cout << val << " ";
}//仿函數(shù)(函數(shù)對象)本身是個類。不是一個函數(shù)
class print02
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}for_each(v.begin(), v.end(), print01);//第三個位置,普通函數(shù)是把函數(shù)名放過來cout << endl;for_each(v.begin(), v.end(), print02());//第三個位置需要傳入函數(shù)對象//類名加小括號,創(chuàng)建出匿名對象
}
int main()
{test01();system("pause");return 0;
}
1.2transform
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用遍歷算法 transform//仿函數(shù)(函數(shù)對象)本身是個類。不是一個函數(shù)
class Transform
{
public://搬運過程中把每個元素取出來在返回回去,由于操作的是int型,所以返回intint operator()(int val){return val+100;//+100在搬到容器中}
};
class Myprint
{
public:void operator()(int val){cout << val << " ";}
};
void test01()
{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());//最后一個位置函數(shù)對象for_each(vTarget.begin(), vTarget.end(), Myprint());//最后一個位置函數(shù)對象cout << endl;
}
int main()
{test01();system("pause");return 0;
}
2.常用查找算法
2.1find,返回值是迭代器
2.1.1查找內(nèi)置數(shù)據(jù)類型?
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find//查找 內(nèi)置數(shù)據(jù)類型
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找 容器中 是否有 5 這個元素vector<int>::iterator it = find(v.begin(), v.end(), 50);if (it == v.end()){cout << "沒有找到!" << endl;}else{cout << "找到:" << *it << endl;}
}int main()
{test01();system("pause");return 0;
}
?2.1.2查找自定義數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//findclass Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}//重載== 讓底層find知道如何對比person數(shù)據(jù)類型bool operator ==(const Person& p)//const防止修改p{if (this->m_Name == p.m_Name && this->m_Age == p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
//查找 自定義數(shù)據(jù)類型
void test02()
{vector<Person>v;//創(chuàng)建數(shù)據(jù)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("bbb", 20);//查找是否有和p一樣的vector<Person>::iterator it = find(v.begin(), v.end(), p);if (it == v.end()){cout << "沒有找到" << endl;}else{cout << "找到元素:姓名:" << (*it).m_Name << " 年齡:" << it->m_Age << endl;}}
int main()
{test02();system("pause");return 0;
}
2.2fin_if 按條件查找元素
2.2.1查找內(nèi)置的數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find_if//1.查找內(nèi)置數(shù)據(jù)類型
class GreaterFive
{
public://謂詞返回boolbool operator()(int val)//find_if的底層也是取出每個元素并解引用,放到重載小括號里去操縱{return val > 5;//大于5 的時候就返回真}
};
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//返回一個迭代器vector<int>::iterator it=find_if(v.begin(), v.end(), GreaterFive());//第三個位置是匿名函數(shù)對象if (it == v.end()){cout << "沒有找到大于5的元素" << endl;}else{cout << "找到大于5的數(shù)字為:" << *it << endl;}
}//2.查找自定義數(shù)據(jù)類型int main()
{test01();system("pause");return 0;
}
2.2.2查找內(nèi)置數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<string>
//常用查找算法
//find_if//2.查找自定義數(shù)據(jù)類型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};
class Great20
{
public:bool operator()(Person &p)//每個數(shù)據(jù)類型都是Perosn的數(shù)據(jù)類型用引用的方式傳進來{return p.m_Age > 20;}
};
bool G2(Person& p)
{return p.m_Age > 20;
}
void test02()
{vector<Person>v;//創(chuàng)建數(shù)據(jù)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);//找年齡大于20的人vector<Person>::iterator it = find_if(v.begin(), v.end(), Great20());if (it == v.end()){cout << "沒有找到" << endl;}else{cout << "找到姓名:" << (*it).m_Name << "年齡:" << it->m_Age << endl;}vector<Person>::iterator it1 = find_if(v.begin(), v.end(), Great20());if (it1 == v.end()){cout << "沒有找到" << endl;}else{cout << "找到姓名:" << (*it1).m_Name << "年齡:" << it1->m_Age << endl;}
}
int main()
{test02();system("pause");return 0;
}
2.3查找相鄰元素adjeacent_find
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//adjacent_find
void test01()
{vector<int>v;v.push_back(0);v.push_back(2);v.push_back(0);v.push_back(3);v.push_back(1);v.push_back(4);v.push_back(3);v.push_back(3);vector<int>::iterator it=adjacent_find(v.begin(), v.end());if (it == v.end()){cout << "未找到相鄰重復元素" << endl;}else{cout << "找到相鄰重復元素:" << *it << endl;}
}int main()
{test01();system("pause");return 0;
}
2.4查找指定元素是否存在binarary_search
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//binary_search 二分查找法,在無序的序列中不可以用
void test01()
{vector<int>v;for (int i = 0; i < 10; i++){v.push_back(i);}//查找容器中是否有9//注意容器必須是有序的序列//如果無序結果未知bool ret = binary_search(v.begin(), v.end(), 9);if (ret){cout << "找到了元素" << endl;}else{cout << "沒找到" << endl;}
}int main()
{test01();system("pause");return 0;
}
2.5統(tǒng)計元素的個數(shù)count
?2.5.1統(tǒng)計內(nèi)置數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count//1.統(tǒng)計內(nèi)置數(shù)據(jù)類型
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(40);v.push_back(20);v.push_back(40);int num=count(v.begin(), v.end(), 40);cout << "40的元素個數(shù)為:" <<num<< endl;int num1 = count(v.begin(), v.end(), 1);cout << "1的元素個數(shù)為:" << num1 << endl;//輸出0
}int main()
{test01();system("pause");return 0;
}
2.5.2統(tǒng)計自定義數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count//2.統(tǒng)計自定義數(shù)據(jù)類型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}bool operator==(const Person& p)//底層要加const,{if (m_Age==p.m_Age){return true;}else{return false;}}string m_Name;int m_Age;
};
void test02()
{vector<Person>v;Person p1("劉備", 35);Person p2("關羽", 35);Person p3("張飛", 35);Person p4("趙云", 30);Person p5("曹操", 40);//將人員插入到容器中v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);Person p("諸葛亮", 35);//統(tǒng)計與諸葛亮年齡相同的有幾人int num = count(v.begin(), v.end(), p);cout << "和諸葛亮同歲數(shù)的人員個數(shù)為:" << num << endl;
}int main()
{test02();system("pause");return 0;
}
2.6按條件統(tǒng)計元素個數(shù)
2.6.1統(tǒng)計內(nèi)置數(shù)據(jù)類型?
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count_if//1.統(tǒng)計內(nèi)置數(shù)據(jù)類型
class Greater20
{
public:bool operator()(int val){return val > 20;}
};
void test01()
{vector<int>v;v.push_back(10);v.push_back(40);v.push_back(30);v.push_back(20);v.push_back(40);v.push_back(20);int num = count_if(v.begin(), v.end(), Greater20());cout << "大于20的元素個數(shù)為:" << num << endl;
}int main()
{test01();system("pause");return 0;
}
2.6.2統(tǒng)計自定義的數(shù)據(jù)類型
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用查找算法
//count_if//2.統(tǒng)計自定義的數(shù)據(jù)類型
class Person
{
public:Person(string name, int age){m_Name = name;this->m_Age = age;}string m_Name;int m_Age;
};class AgeGreater20
{
public:bool operator()(Person &p){return p.m_Age > 20;}
};void test02()
{vector<Person>v;Person p1("劉備", 35);Person p2("關羽", 35);Person p3("張飛", 35);Person p4("趙云", 40);Person p5("曹操", 20);v.push_back(p1);v.push_back(p2);v.push_back(p3);v.push_back(p4);v.push_back(p5);//統(tǒng)計 大于20歲人員個數(shù)int num = count_if(v.begin(), v.end(), AgeGreater20());cout << "大于20的元素個數(shù)為:" << num << endl;
}int main()
{test02();system("pause");return 0;
}
3.常用排序算法
3.1sort
#include<iostream>
using namespace std;
#include<vector>
#include<algorithm>
//常用排序算法
//sort
void myPrint(int val)
{cout << val << " ";
}
void test01()
{vector<int>v;v.push_back(10);v.push_back(30);v.push_back(50);v.push_back(20);v.push_back(40);//利用sort進行升序,默認情況下升序sort(v.begin(), v.end());for_each(v.begin(), v.end(), myPrint);cout << endl;//改變?yōu)榻敌騭ort(v.begin(), v.end(), greater<int>());//greater<int>()內(nèi)建函數(shù)對象,需要包含functional頭文件,編譯器高的不包含functional頭文件也不會出錯for (int i = 0; i < v.size(); i++){cout << v[i] << " ";}cout << endl;
}int main()
{test01();system("pause");return 0;
}
bool compare(int a,int b)
{ return a < b; //升序排列,如果改為return a>b,則為降序
}
int a[20]={2,4,1,23,5,76,0,43,24,65},i;
for(i=0;i<20;i++) cout<< a[i]<< endl;
sort(a,a+20,compare);
#include<iostream>
using namespace std;
#include<stack>
#include<algorithm>
#include<bitset>
#include<cmath>
#include<queue>
#include<set>
#include<map>struct Point
{int x;int y;//Point(int xx, int yy) :x(xx), y(yy) {};bool operator < (Point& p) {if (x != p.x) {return x < p.x;} else {return y < p.y;}}
};int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end());for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*輸出:1 21 32 1*/system("pause");return 0;
}--------------------------------------------------------------------------------------struct Point
{int x;int y;
};
bool Cmp(Point& p1, Point& p2) {if (p1.x != p2.x) {return p1.x < p2.x;} else {return p1.y < p2.y;}
}int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end(),Cmp);for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*輸出:1 21 32 1*/system("pause");return 0;
}
----------------------------------------------------------------------------------------struct Point
{int x;int y;
};
class cmp
{
public:bool operator()(Point& p1, Point& p2)const {if (p1.x != p2.x) {return p1.x < p2.x;} else {return p1.y < p2.y;}}
};int main()
{vector<Point> p;p.push_back(Point{ 1,2 });p.push_back(Point{ 1,3 });Point p1;p1.x = 2;p1.y = 1;p.push_back(p1);sort(p.begin(), p.end(), cmp());for (int i = 0; i < p.size(); i++) {cout << p[i].x << " " << p[i].y << endl;}/*輸出:1 21 32 1*/system("pause");return 0;
}