目录
包含算法头文件algorithm
一、查找算法
#include<iostream>
#include<algorithm>
#include<string>
#include<functional>
#include<vector>
#include<list>
(1)基本查找
find:区间查找
find_if:条件查找
find_firt_of: 查找区间第一次出现值
adjacent_find: 查找第一次重复的数
search:子序列查找
search_n: 子序列查找出现次数
//1.find查找
int array[] = { 1,4,6,2,3,0,5,7,9,8 };
vector<int> vec;//复制array
vec.assign(array, array + 10);
//对于大部分查找返回迭代器
auto result = find(vec.begin(), vec.end(), 9);
//未找到返回的是容器结束为止
if(result!=vec.end())
cout << *result << endl;
//2.find_if
//第一个比三大的
auto findif = find_if(vec.begin(), vec.end(), [](int x) {return x > 3;});
cout << *findif << endl;
//3.find_first_of 第一个相等的 和string不同
int temp[] = { 99,2,1 };
//在vec中桉顺序取出数据与temp所有数据比对直到两个个数据相同,在第一次相同时结束返回相同的数据
auto findFirstOf = find_first_of(vec.begin(), vec.end(), temp, temp + 3);//返回1
cout << *findFirstOf << endl;
//4查找第一个重复的元素
vector<int> reVec = { 1,5,3,7,7,5,3,3,1 };
auto reIter = adjacent_find(reVec.begin(), reVec.end());
cout << *reIter << endl;
//5.找子串(顺序也要一直)
int searchData[] = { 5,3,3 };
auto it = search(reVec.begin(), reVec.end(), searchData, searchData + 3);
//如果找到 返回子串第一个数字
cout << *it << endl;
//6找重复出现的元素
//如果出现不明确 使用std::标识
//表示查找是否存在两个连续的7 2表示连续2个 7表示值的大小为7
auto sn = search_n(reVec.begin(), reVec.end()-1,2,7);
cout << *sn << endl;
//找到三个满足lanbda表达式的值
sn = search_n(reVec.begin(), reVec.end(), 3, 3, [](int a, int b) {return a > b;});
cout << *sn << endl;
(2)统计查找
count: 区间统计
count_if: 条件统计个数
equal:比较
//7出现的次数
cout << count(reVec.begin(), reVec.end(), 7) << endl;
//8条件统计
cout << count_if(reVec.begin(), reVec.end(), [](int x) {return x > 6;})<<endl;
//9比较
list<int> myList = { 1,2,3,4 };
//boolalpha表示打印true false
cout << boolalpha << equal(reVec.begin(), reVec.end(), myList.begin(), myList.end())<<endl;
(3)有序查找
binary_search:二分查找
upper_bound: 查找最后一个大于查找的值
lower_bound: 大于等于查找的值
equal_range:区间比较---有序序列
//有序操作(原容器数据必须有序)
//10二分查找
vector<int> testData = { 1,2,3,4,5,6,7,8,9 };
cout << boolalpha << binary_search(testData.begin(), testData.end(), 2);
//11.大于
auto uper = upper_bound(testData.begin(), testData.end(), 6);
cout << *uper << endl;
//12大于等于
auto low = lower_bound(testData.begin(), testData.end(), 6);
cout << *low << endl;
//13.equal_range 包含上面的两个结果
// 返回的是pair类型
auto er = equal_range(testData.begin(), testData.end(), 6);
cout << *er.first << " " << *er.second << endl;
二、排序算法
#include<iostream>
#include<algorithm>
#include<string>
#include<functional>
#include<vector>
#include<list>
#include<ctime>
merge: 归并排序,存于新容器
inplace_merge: 归并排序,覆盖原区间
sort: 排序,更改原容器顺序
stable_sort: 排序,保存原容器数据顺序
nth_element: 关键字排序
partition:范围排序
partial_sort:范围排序
辅助打印函数
template <class _Ty>
void print(_Ty data)
{
for (auto v : data)
{
cout << v << " ";
}
cout << endl;
}
(1)sort算法
class MM
{
public:
MM(string name, int age) :name(name), age(age) {}
void print()
{
cout << name << " " << age << endl;
}
int getAge()
{
return age;
}
string getName()
{
return name;
}
protected:
string name;
int age;
};
void printSortUserData()
{
//1.sort算法
vector<int> vec = { 1,2,3,4,5,6,7,8,9 };
sort(vec.begin(), vec.end());//默认从小到大
print(vec);
sort(vec.begin(), vec.end(), greater<int>());
print(vec);
vector<MM> mmData;
mmData.push_back(MM("as", 12));
mmData.push_back(MM("bs", 13));
mmData.push_back(MM("cs", 14));
sort(mmData.begin(), mmData.end(), [](auto a, auto b)->auto {return a.getAge() < b.getAge();});
for (auto v : mmData)
{
v.print();
}
}
(2)
//2.保持元素的相对位置排序
bool com_as_int(double a, double b)
{
return int(a) < int(b);
}
void tetsStableSort()
{
vector<double> num = { 1.33,2.33,1.22,3.22,3.44,3.66 };
stable_sort(num.begin(), num.end(), com_as_int);
//并没有按照从小到大的顺序
print(num);
//7.保持相对顺序不变
stable_partition(num.begin(), num.end(), bind(com_as_int, std::placeholders::_1, 2.0));
print(num);
}
(3)归并
//3.归并排序
void testMerge()
{
//3.不会更改原容器的归并
vector<int> vec = { 1,2,3,4,5,0,6,7,8,9 };
vector<int> result(vec.size());
//将两段有序序列归并到一个容器中
merge(vec.begin(), vec.begin() + 5, vec.begin() + 5, vec.end(), result.begin());
print(result);
//4.会改变原容器的归并
inplace_merge(vec.begin(), vec.begin() + 5, vec.end());
print(vec);
}
(4)关键字
//5、关键字排序
vector<int> test = { 1,2,3,4,5,0,6,8,7,9 };
//以5为基准排序
nth_element(test.begin(), test.begin() + 5, test.end());
print(test);
(5)分类处理
//6分类处理
//通过子函数描述条件,返回true放在左边,返回false放在右边
//只是做分类,不排序
vector<int> score = { 98,10,98,23,45,65,34,56,67 };
partition(score.begin(), score.end(), [](int a) {return a > 60;});
print(score);
(6)局部排序
//8.局部排序
vector<int> num = { 22,33,45,8,23,66,45,34,244 };
//前五个有序
partial_sort(num.begin(), num.begin() + 5, num.end());
print(num);
//9局部排序,结果另存
vector<int> result(5);
partial_sort_copy(num.begin(), num.begin() + 5, result.begin(), result.end());
print(result);
(7)乱序算法
//10.乱序算法 不会出现重复
srand((unsigned int)time(nullptr));
random_shuffle(num.begin(), num.end());
print(num);
(8)逆序
//11.逆序反转
vector<int> re = { 1,2,3,4,5,0,6,8,7,9 };
reverse(re.begin(), re.end());
print(re);
//12反转另存
vector<int> rec(re.size());
reverse_copy(re.begin(), re.end(), rec.begin());
print(rec);
(9)移位
//13.移位操作
vector<int> move = { 1,2,3,4,5,6 };
rotate(move.begin(), move.begin() + 2, move.end());
print(move);
//14移位另存
vector<int> moveResult(move.size());
rotate_copy(move.begin(), move.begin() + 3, move.end(), moveResult.begin());
print(moveResult);
三、拷贝删除修改类
#include<iostream>
#include<algorithm>
#include<string>
#include<functional>
#include<vector>
#include<list>
copy: 拷贝函数
copy_backward: 逆序拷贝
remove: 删除
remove_copy: 删除元素复制到新容器
remove_if:条件删除
remove_copy_if:条件删除拷贝到新容器
replace:替换
replace_copy: 替换,结果放到新容器
replace_if: 条件替换
replace_copy_if:条件替换,结果另存
swap: 交换
swap_range:区间交换
iter_swap: 交换
unique:去重
unique_copy:去重,结果另存
辅助打印函数
template<class _Ty>
void print(_Ty data)
{
for (auto v : data)
{
cout << v << " ";
}
cout << endl;
}
(1)拷贝
//list的创建没有长度,使用链表的方式,创建之初无长度
//1正向拷贝
vector<int> vec = { 1,2,3,4,5,6 };
vector<int> nvec(vec.size());
copy(vec.begin(), vec.end(), nvec.begin());
print(nvec);
//2逆向拷贝 从后面开始赋值
vector<int> rvec(vec.size());
copy_backward(vec.begin(), vec.end(), rvec.end());
print(rvec);
(2)删除
//3删除 不会影响容器大小 是伪删除 如果元素不存在返回容器结束的位置
remove(vec.begin(), vec.end(), 3);
print(vec);
//4伪删 另存
vector<int> result(vec.size()-1 );
remove_copy(vec.begin(), vec.end(), result.begin(), 2);
print(result);
//5条件删除
vector<int> score = { 98,45,65,77,45,43 };
remove_if(score.begin(), score.end(), [](int x) {return x < 60;});
print(score);
//6条件删除另存
vector<int> ok(count_if(score.begin(), score.end(), [](int x) {return x >= 60;}));
remove_copy_if(score.begin(), score.end(), ok.begin(), [](int x) {return x < 60;});
print(ok);
(3)替换
//7替换算法(全部修改)
vector<int> rep = { 1,2,3,4,5,6,7,1,2,3,1,2,3 };
replace(rep.begin(), rep.end(), 1, 100);
print(rep);
//8替换后结果另存
vector<int> repc(rep.size());
replace_copy(rep.begin(), rep.end(), repc.begin(), 100, 0);
print(repc);
//9条件替换
replace_if(rep.begin(), rep.end(), [](int x) {return x < 6;}, 0);
print(rep);
//10条件替换,结果另存
vector<int> repif(rep.size());
replace_copy_if(rep.begin(), rep.end(), repif.begin(), [](int x) {return x == 0;}, 1);
print(repif);
(4)交换
//11交换
vector<int> test = { 11,2,3,4,5,6,7,8,9 };
iter_swap(test.begin(), test.end() - 1);
print(test);
//12 不需要两个容器长度一样
vector<int> test2 = { 2,2 };
swap(test, test2);
print(test);
print(test2);
cout << endl;
//13区间交换
swap_ranges(test2.begin(), test2.begin() + 1, test.begin());
print(test2);
(5)去重
//14去重(相邻相同的只保留一个) 想去重先排序再去重
vector<int> uniq = { 1,1,3,4,4,3,3,2,2,2,4,4,4 };
unique(uniq.begin(), uniq.end());
print(uniq);
//15去重另存
vector<int> uniqr(uniq.size());
unique_copy(uniq.begin(), uniq.end(), uniqr.begin());
print(uniqr);