您的位置:首页 > 编程语言 > C语言/C++

C++ STL 常用算法

2017-10-10 14:32 351 查看
STL算法太多了, 我这里整理几个常用的

#include <iostream>
#include <algorithm>
#include <vector>
#include <list>
#include <time.h>
#include <functional>
#include <numeric>
using namespace std;

void printC(vector<int> &v)
{
for(vector<int>::iterator it = v.begin(); it<v.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}

template <typename T>
class ShowE
{
public:
void operator()(T t)
{
cout << t << " ";
}
};

// 遍历算法
// for_each
void func6_1()
{
vector <int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(7);
v.push_back(9);
printC(v);

for_each(v.begin(), v.end(), ShowE<int>());
cout << endl;
}

int increase(int a)
{
return a+100;
}

// 转换算法
// transfrom
void func6_2()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(7);
v.push_back(9);

// transform 需要的函数要有返回值, for_each 需要的参数是引用 不需要返回值
transform(v.begin(), v.end(), v.begin(), increase);
printC(v);

// 取反
transform(v.begin(), v.end() , v.begin(), negate<int>());
printC(v);

// 放到别的容器中
list<int> ls;
ls.resize(v.size());

//transform(v.begin(), v.end(), ls.begin(), increase);
transform(v.begin(), v.end() , ls.begin(), bind2nd(multiplies<int>(), 100));
for_each(ls.begin(), ls.end(), ShowE<int>());
cout << endl;
}

// 查找算法
// adjacent_find
void func6_3()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(5);
v.push_back(7);
v.push_back(7);
v.push_back(9);

// 在容器中查找是否有重复的元素, 返回第一个重复的元素的迭代器
// 这里 输出 5
vector<int> :: iterator it = adjacent_find(v.begin(), v.end());
if(it != v.end())
cout << *it << endl;
}

//binary_search
void func6_4()
{
vector<int> v;
v.push_back(1);
v.push_back(3);
v.push_back(5);
v.push_back(5);
v.push_back(7);
v.push_back(9);

// binary_search 二分法查找, 注意: 只可以在有序数据中查找, 不然没用
bool b = binary_search(v.begin(), v.end(), 7);
if(b)
cout << "找到" << endl;
else
cout << "没有找打" << endl;
}

// 排序算法
// merge 合并排序
// random_shuffle 随机排序
// reverse 倒置排序
void func6_5()
{
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());
printC(v3);

srand((unsigned int)time(NULL));

// 随机排序, 需要设置随机种子
random_shuffle(v3.begin(), v3.end());
printC(v3);

// 倒置排序
reverse(v3.begin(), v3.end());
printC(v3);

// 复制算法
vector<int> v4(v3.size()); // 默认全是0
copy(v3.begin(), v3.begin()+5, v4.begin());
v4[2] = 0;
v4[3] = 0;
printC(v4);

// 替换算法
// 把零全部换成10
replace(v4.begin(), v4.end(), 0 ,10);
printC(v4);

replace_if(v4.begin(), v4.end(), bind2nd(greater<int>(), 2), 7);
printC(v4);

// 交换算法 可以用于清空容器
vector<int> v5;
swap(v3,v5);

cout << "v3:" << endl;
printC(v3);

printC(v4);
// 求和算法
int sum = accumulate(v4.begin(), v4.end(), 100);
cout << sum << endl;

// 区域改变
// 注意 这只之改变两个元素
fill(v4.begin(), v4.begin()+2, 100);
printC(v4);
}

int main()
{
//func6_1();
//func6_2();
//func6_3();
//func6_4();
func6_5();
system("pause");
return 0;
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: