您好,登錄后才能下訂單哦!
1、STL查找算法
(1)、代碼如下:
#include<iostream> #include<vector> #include<algorithm> using namespace std; //查找算法!?。?void main_adjacent_find(){ vector<int> v1; v1.push_back(1); v1.push_back(2); v1.push_back(2); v1.push_back(3); v1.push_back(5); //查找第一個(gè)元素重復(fù)的位置; vector<int>::iterator it; it = adjacent_find(v1.begin(), v1.end()); //查找第一個(gè)重復(fù)出現(xiàn)的數(shù)字; if(it == v1.end()){ cout<<"沒有找到重復(fù)的元素"<<endl; }else{ cout<<*it<<endl; } int index = distance(v1.begin(), it); //從頭開始,的知當(dāng)前迭代器的指針?biāo)诘臄?shù)組下標(biāo)處; cout<<"index:"<<index<<endl; } void main_binary_search(){ vector<int> v1; //0 1 2 3 4 5.......n-1 //二分法,10次就可以找到了; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(9); bool b = binary_search(v1.begin(), v1.end(), 7); if(b == true){ cout<<"找到了"<<endl; }else{ cout<<"沒找到"<<endl; } } void main_count(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(7); v1.push_back(9); v1.push_back(7); int num = count(v1.begin(), v1.end(), 7); cout<<"num:"<<num<<endl; } bool GreatThree(int num){ if(num > 3){ return true; } return false; } void main_count_if(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(9); int num = count_if(v1.begin(), v1.end(), GreatThree); cout<<"num:"<<num<<endl; } void main_find(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(9); vector<int>::iterator it; it = find(v1.begin(), v1.end(), 5); cout<<*it<<endl; } void main_find_if(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); v1.push_back(9); vector<int>::iterator it; it = find_if(v1.begin(), v1.end(), GreatThree); //返回的是第一個(gè)回調(diào)函數(shù)所指向的迭代器; cout<<*it<<endl; } int main(void){ main_adjacent_find(); //查找第一個(gè)重復(fù)的元素; main_binary_search(); //二分法查找某一個(gè)元素; main_count(); //查找某一個(gè)元素出現(xiàn)的次數(shù); 針對(duì)基礎(chǔ)數(shù)據(jù)類型 main_count_if(); //用一個(gè)一元謂詞,可以查找大于某一個(gè)數(shù)字的個(gè)數(shù); 針對(duì)自定義的數(shù)據(jù)類型 main_find(); //查找某一數(shù)據(jù),通過迭代器指出; 針對(duì)基礎(chǔ)數(shù)據(jù)類型 main_find_if(); //查找某一數(shù)據(jù),通過迭代器指出; 針對(duì)自定義數(shù)據(jù)類型; return 0; }
(2)、運(yùn)行結(jié)果:
2、STL排序算法
(1)、代碼如下:
#include<iostream> #include<string> #include<vector> #include<algorithm> #include<functional> using namespace std; //排序算法 void printV(vector<int> &v){ vector<int>::iterator it; for(it = v.begin(); it != v.end(); it++){ cout<<*it<<" "; } cout<<endl; } void main_merge(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); vector<int> v2; v2.push_back(2); v2.push_back(4); v2.push_back(6); vector<int> v3; v3.resize(v1.size() + v2.size()); merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); printV(v3); } class Student{ public: Student(string name, int id){ m_name = name; m_id = id; } public: string m_name; int m_id; }; bool Compare(Student s1, Student s2){ return (s1.m_id < s2.m_id); } void main_sort(){ Student s1("老大", 1); Student s2("老二", 2); Student s3("老三", 3); Student s4("老四", 4); vector<Student> v1; v1.push_back(s4); v1.push_back(s1); v1.push_back(s3); v1.push_back(s2); vector<Student>::iterator it; for(it = v1.begin(); it != v1.end(); it++){ cout<<it->m_id<<"\t"<<it->m_name<<endl; } //sort()要對(duì)自定義的數(shù)據(jù)類型進(jìn)行排序,就的進(jìn)行自定義數(shù)據(jù)類型的排序; sort(v1.begin(), v1.end(), Compare); //Compare就是謂詞,謂詞就是函數(shù)名稱,返回值為bool類型; for(it = v1.begin(); it != v1.end(); it++){ cout<<it->m_id<<"\t"<<it->m_name<<endl; } } void main_random_shuffle(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); //對(duì)動(dòng)態(tài)數(shù)組進(jìn)行隨機(jī)打亂 random_shuffle(v1.begin(), v1.end()); printV(v1); //對(duì)字符串隨機(jī)的打亂順序; string str = "abcdefg"; random_shuffle(str.begin(), str.end());; cout<<"str:"<<str<<endl; } void main_reverse(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); v1.push_back(7); reverse(v1.begin(), v1.end()); printV(v1); } int main(void){ main_merge(); //歸并排序 main_sort(); main_random_shuffle(); main_reverse(); return 0; }
(2)、運(yùn)行結(jié)果:
3、STL拷貝替換
(1)、代碼如下:
#include<iostream> #include<numeric> //accumulate()函數(shù)的頭文件; #include<vector> #include<algorithm> #include<functional> using namespace std; //拷貝替換 void printV(vector<int> &v){ vector<int>::iterator it; for(it = v.begin(); it != v.end(); it++){ cout<<*it<<" "; } cout<<endl; } void main_copy(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); vector<int> v2; v2.resize(v1.size()); //調(diào)整容器大小的函數(shù); copy(v1.begin(), v1.end(), v2.begin()); //拷貝容器函數(shù); printV(v2); } void main_replace(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(3); v1.push_back(3); v1.push_back(5); replace(v1.begin(), v1.end(), 3, 8); //替換函數(shù),將前面的數(shù)據(jù)都用后面的數(shù)據(jù)替換; printV(v1); } bool great_equal(int &n){ if(n >= 5){ return true; }else{ return false; } } void main_replace_if(){ vector<int> v1; v1.push_back(1); v1.push_back(8); v1.push_back(8); v1.push_back(8); v1.push_back(5); replace_if(v1.begin(), v1.end(), great_equal, 1); //這里就是可以替換范圍內(nèi)的數(shù)據(jù),通過謂詞進(jìn)行控> 制!!,將great_equal范圍內(nèi)的數(shù)字(返回值為真的)進(jìn)行替換!!! printV(v1); } void main_swap(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); vector<int> v2; v2.push_back(2); v2.push_back(4); v2.push_back(6); swap(v1, v2); //交換了v1、v2容器中的內(nèi)容!!! printV(v1); } void main_accumulate(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); int tmp = accumulate(v1.begin(), v1.end(), 100); //求和函數(shù),加上一個(gè)初始值100; cout<<tmp<<endl; } void main_fill(){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); fill(v1.begin(), v1.end(), 8); //將容器中的數(shù)值全都改為8; printV(v1); } int main(void){ main_copy(); //拷貝 main_replace(); //替換 基礎(chǔ)數(shù)據(jù)類型 main_replace_if(); //替換范圍 自定義數(shù)據(jù)類型 main_swap(); //交換容器中的內(nèi)容 main_accumulate(); //求和函數(shù) main_fill(); return 0; }
(2)、運(yùn)行結(jié)果:
4、STL集合算法
(1)、代碼如下:
#include<iostream> #include<algorithm> #include<functional> #include<vector> using namespace std; void printV(vector<int> &v){ vector<int>::iterator it; for(it = v.begin(); it != v.end(); it++){ cout<<*it<<" "; } cout<<endl; } //集合 int main(void){ vector<int> v1; v1.push_back(1); v1.push_back(3); v1.push_back(5); vector<int> v2; v2.push_back(1); v2.push_back(1); v2.push_back(1); vector<int> v3; v3.resize(v1.size() + v2.size()); set_union(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin()); //集合中取并集;沒有填滿的空間默,認(rèn)為0; printV(v3); return 0; }
(2)、運(yùn)行結(jié)果:
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。