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;
}
#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;
}
相关文章推荐
- 【算法专题】工欲善其事必先利其器—— C++ STL中vector(向量/不定长数组)的常用方法总结
- C++中STL常用算法概要
- C++_STL—较为常用的算法
- 算法竞赛中常用的C++ STL
- C++:STL常用函数模块总结(算法algorithm)
- C++ STL 常用算法
- c++ STL常用算法使用方法
- C++ STL常用算法与容器
- 【C++研发面试笔记】21. 常用算法-STL中常用算法函数
- STL常用算法: remove,remove_if,remove_copy,remove_copy_if,remove系列算法和partition的区别...
- C++ STL 算法另类使用方法
- c++常用算法收集
- 【C++】STL常用容器总结之一:容器与迭代器
- stl常用算法_查找相关
- 浅析STL中的常用算法
- C++——STL所有算法介绍
- c++ stl string 常用函数
- 期末考试 编程题#11:数据库内的学生信息(Coursera 程序设计与算法 专项课程3 C++程序设计 郭炜、刘家瑛;STL编写)
- STL中常用的一些算法
- C++ STL 算法列表