算法頭文件
小劉:親情提示沒有對應的頭文件,算法是不能使用的哦
#include< algorithm >
是STL頭文件最大的一個:涉及到 比較 查找 排序 遍歷 複製 修改#include< functional>
定義了一些模板類,以供使用#include< numeric>
體積很小,只包括幾個所學運算的模板函數
算法
宇哥:重點來了,注意啊
常用遍歷算法(頭文件algorithm)
for_each(iteraror begin,iterator end,_func)
// 遍歷容器 _func爲函數
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
void print(int n){
cout << n << " ";
}
int main() {
vector<int> t;
t.push_back(10);
t.push_back(20);
t.push_back(30);
t.push_back(40);
t.push_back(50);
t.push_back(60);
for_each(t.begin(), t.end(), print);
}
transfrom(iterator begin1,iterator end1,iterator begin2,iterator end2,_func)
//搬運一個容器到另一個容器
常用查找算法(頭文件algorithm)
find(iterator begin,iterator end,_func)
//尋找元素是否存在,一定要重載==, 返回迭代器
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class T {
public:
int a;
//在使用find 時 一定要重載==號
bool operator==(const T& t) {
if (this->a == t.a) {
return true;
}
else
false;
}
};
int main() {
vector<T> t;
T t1, t2, t3;
t1.a = 10;
t2.a = 5;
t3.a = 5;
t.push_back(t1);
t.push_back(t2);
t.push_back(t3);
//find 查找元素是否存在 返回迭代器
vector<T>::iterator iter1 = find(t.begin(), t.end(), t1);
if (iter1 != t.end()) {
cout << iter1->a<<endl;//10
}
else {
cout << "未找到該元素"<<endl;
}
}
- **
find(iterator begin,iterator end,_Pred)
//按 條件尋找元素,_Pred爲函數或謂詞
adjacent_find(iterator begin,iterator end,_Pred)
查找是否有相鄰重複元素存在
#include <iostream>
#include<vector>
#include<algorithm>
using namespace std;
class T {
public:
int a;
bool operator=(const T& t) {
if (this->a == t.a) {
return true;
}
else
false;
}
};
class compare {
public:
bool operator()(T t1, T t2) {
if (t1.a == t2.a) {
return true;
}
return false;
}
};
int main() {
vector<T> t;
T t1, t2, t3;
t1.a = 10;
t2.a = 5;
t3.a = 5;
t.push_back(t1);
t.push_back(t2);
t.push_back(t3);
//adjacent_find 尋找相鄰重複元素
vector<T>::iterator iter2 = adjacent_find(t.begin(), t.end(), compare());
if (iter2 != t.end()) {
cout << iter2->a << endl;//5
}
else {
cout << "未找到相鄰重複元素" << endl;
}
}
binary_search(iterator begin,iterator end,_Pred)
//二分法查找,容器必須有序
cout(iterator begin,iterator end,vaule)
//查找元素個數cout(iterator begin,iterator end,_Pred)
//按條件查找元素個數
排序算法(頭文件algorithm)
sort(iterator begin,iterator end,_Pred)/
/按照要求排序
random_shuffle(iterator begin,iterator end)
//打亂容器內元素位置,隨機洗牌
marge(iterator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)
//將兩個容器合併到目標的容器裏,目標容器需提前開闢空間
reserve(iterator begin,iterator end)
//將容器中元素顛倒
拷貝替換算法(頭文件algorithm)
拷貝過程中,須給新容器用resize提前開闢空間
copy(iterator begin,iterator end,iterator dest)
//將一個容器中元素拷貝到另一個容器
replace(iterator begin,iterator end,oldvalue,newvalue)
//將容器中舊元素改爲新元素replace_if(iterator begin,iterator end,_Pred,newvalue)
//將容器中舊元素按條件改爲新元素
swap(iterator begin1,iterator end1,iterator begin2,iterator end2)
//互換兩個容器元素
常用集合算法(頭文件algorithm)
set_intersection(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)
//求兩個容器的交集
set_union(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)
//求兩個容器的並集
set_difference(terator begin1,iterator end1,iterator begin2,iterator end2,iterator dest)
//求兩個容器的差集
常用算數生成算法(頭文件numeric)
accumulate(iterator begin,iterator end,value)
//容器中元素累加 value 爲起始累加值
fill(iterator begin,iterator end,value)
//容器中用value填充
C++入門到此徹底結束,完結撒花,拜拜了你