#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
#include <string>
#include <array>
#include <ctime>
#include <cstdlib>
#include <random>
#include <list> int main()
{
std::vector<int> vec{, , , , , }; //1.简单查找 //return iterator by value/unaryPred
//查找单值出现位置
std::cout<<*std::find(vec.begin(),vec.end(),)<<std::endl;
std::cout<<*std::find_if(vec.begin(),vec.end(),[](int& value){return value==;})<<std::endl;
std::cout<<*std::find_if_not(vec.begin(),vec.end(),[](int& value){return value==;})<<std::endl; //return times by value/unaryPred
std::cout<<std::count(vec.begin(),vec.end(),)<<std::endl;
std::cout<<std::count_if(vec.begin(),vec.end(),[](int& value){return value==;})<<std::endl; //return bool by unaryPred;
//对序列元素调用一元谓词
std::cout<<std::boolalpha;
std::cout<<std::all_of(vec.begin(),vec.end(),[](int& value){return typeid(value)==typeid(int&);})<<std::endl;
std::cout<<std::any_of(vec.begin(),vec.end(),[](int& value){return value==;})<<std::endl;
std::cout<<std::none_of(vec.begin(),vec.end(),[](int& value){return value==;})<<std::endl;
std::cout<<std::noboolalpha; //2.查找重复值 //return iterator by NULL/binaryPred
//返回查找第一个相邻重复元素迭代器
std::cout<<*std::adjacent_find(vec.begin(),vec.end())<<std::endl;
std::cout<<*std::adjacent_find(vec.begin()+,vec.end(),[](int& a,int& b){return a==b;})<<std::endl; //return iterator by binaryPred
//查找某个数是否出现一定次数,返回第一个元素迭代器
std::cout<<*std::search_n(vec.begin(),vec.end(),,)<<std::endl;
std::cout<<*std::search_n(vec.begin(),vec.end(),,,[](int a,int b){ return a==b;})<<std::endl; //3.查找子序列方法 //return iterator by NULL/binaryPred
//从第一个序列中查找第二个序列,返回第二个序列在第一个序列出现的迭代器
std::vector<int> vec_cm{,,,,,};
std::cout<<*std::search(vec.begin(),vec.end(),vec_cm.begin(),vec_cm.end())<<std::endl;
std::cout<<*std::search(vec.begin(),vec.end(),vec_cm.begin(),vec_cm.end(),[](int& a,int& b){return a==b;})<<std::endl; //返回一个迭代器指向第二个序列任意元素在第一个序列中出现的位置
std::cout<<*std::find_first_of(vec.begin(),vec.end(),vec_cm.begin(),vec_cm.end())<<std::endl;
std::cout<<*std::find_first_of(vec.begin(),vec.end(),vec_cm.begin(),vec_cm.end(),[](int& a,int& b){return a==b;})<<std::endl; //类似search,返回序列的尾部
//std::find_end(); //其他只读算法 //熟知的std::for_each();
//对序列每个元素指向可调用对象,对返回值忽略
std::for_each(vec.begin(),vec.end(),[](int& value){std::cout<<value<<"\t";});
std::cout<<std::endl; //比较两序列,返回pair first指向一个不同位置迭代器和second指向第二个相同位置迭代器(~.~)
std::pair<std::vector<int>::iterator,std::vector<int>::iterator> pair1=
std::mismatch(vec.begin(),vec.end(),vec_cm.begin());
// std::pair<std::vector<int>::iterator,std::vector<int>::iterator> pair2=
// std::mismatch(vec.begin(),vec.end(),vec_cm.begin(),[](int& a,int& b){});
std::cout<<*pair1.first<<*pair1.second<<std::endl; //判断2序列相等,则返回true
std::cout<<std::boolalpha;
std::cout<<std::equal(vec.begin(),vec.end(),vec_cm.begin())<<std::endl;
std::cout<<std::equal(vec.begin(),vec.end(),vec_cm.begin(),[](int& a,int& b){ return true;})<<std::endl;
std::cout<<std::noboolalpha; // 二分法搜索
// 这些算法要求前向迭代器,这样花费时间类似线性增长而不是对数增长
// 前提序列有序
// 注意:(有毒)
std::cout << *std::lower_bound(vec.begin(), vec.end(), ) << std::endl;
std::cout << *std::upper_bound(vec.begin(), vec.end(), ) << std::endl; //懵- -
auto pair=std::equal_range(vec.begin(),vec.end(),);
std::cout<<*pair.first<<*pair.second<<std::endl; std::cout << std::boolalpha;
std::cout<<std::binary_search(vec.begin(),vec.end(),)<<std::endl;
std::cout << std::noboolalpha; //拷贝算法 std::vector<int> vec3_;
vec3_.resize(vec.size());
//按第一序列范围拷贝;
std::copy(vec.begin(),vec.end(),vec3_.begin());
//按谓词拷贝
std::copy_if(vec.begin(),vec.end(),vec3_.begin(),[](int& value){ return value>;});
//按数量拷贝,返回尾元素
std::copy_n(vec.begin(),vec.size(),vec.begin()); //对元素调用std::move()移动
std::move(vec.begin(),vec.end(),vec.begin()); std::vector<int> vec4_;
vec4_.resize(vec.size());
// //对第一序列执行一元谓词拷贝到第二序列中
// std::transform(vec.begin(),vec.end(),vec4_.begin(),[](int& value){return value>0;});
//
// //对2序列执行二元谓词放入第三序列
std::vector<int> vec5_;
// vec5_.resize(vec.size()+vec4_.size());
// std::transform(vec.begin(),vec.end(),vec4_.begin(),vec5_.begin(),[](int& a,int& b){ return a==b;}); //拷贝一个序列到另一个序列,与oldvalue比较相等的则在新序列中替换成新值
std::replace_copy(vec.begin(),vec.end(),vec4_.begin(),,); //根据一元谓词条件判断那些值需要被替换成新值
std::replace_copy_if(vec.begin(),vec.end(),vec4_.begin(),[](int& value){return value>;},); //合并2序列到第三序列
//默认降序,可自行设置二元谓词比较
//前提:有序序列
std::merge(vec.begin(),vec.end(),vec4_.begin(),vec4_.end(),vec5_.begin());
std::merge(vec.begin(),vec.end(),vec4_.begin(),vec4_.end(),vec5_.begin(),[](int& a,int& b){return a<b;}); //同类型迭代器之间交换
std::vector<int> vec8_{,,,};
std::for_each(vec.begin(),vec.end(),[](int& value){std::cout<<value<<"\t";});
std::cout<<std::endl;
std::for_each(vec8_.begin(),vec8_.end(),[](int& value){std::cout<<value<<"\t";});
std::cout<<std::endl; std::iter_swap(vec.begin(),vec.begin()+);
std::iter_swap(vec.begin(),vec8_.begin()+);
std::swap_ranges(vec8_.begin(),vec8_.end(),vec.begin()); std::for_each(vec.begin(),vec.end(),[](int& value){std::cout<<value<<"\t";});
std::cout<<std::endl;
std::for_each(vec8_.begin(),vec8_.end(),[](int& value){std::cout<<value<<"\t";});
std::cout<<std::endl; //单序列操作replace/replace_if 区别 replace_copy/replace_copy_if
//值对比或者unaryPred查找;
std::vector<int> vec9_{,,,,,,}; std::replace(vec9_.begin(),vec9_.end(),,);
std::cout<<vec9_[]<<std::endl;
std::replace_if(vec9_.begin(),vec9_.end(),[](int& value){return value%==;},); std::for_each(vec9_.begin(),vec9_.end(),[](int &value){std::cout<<value<<"\t";});
std::cout<<std::endl; //划分算法
std::vector<int> sort_vec{,,,,,,,,}; for(auto& iter:sort_vec)std::cout<<iter<<"\t";
//是否符合划分谓词条件,true在前false在后
if(std::is_partitioned(sort_vec.begin(),sort_vec.end(),[](int& value){return value%==;}))
{
std::cout<<"(Partition)"<<std::endl;
}else
{
std::cout<<"(No partition)"<<std::endl;
} //划分操作,返回谓词为true队列的后一位迭代器;全是false的话返回beg;
std::partition(sort_vec.begin(),sort_vec.end(),[](int& value){ return value%==;});
//std::partition_point(...) 返回partition的迭代器- -(好鸡肋); for(auto& iter:sort_vec)std::cout<<iter<<"\t";
if(std::is_partitioned(sort_vec.begin(),sort_vec.end(),[](int& value){return value%==;}))
{
std::cout<<"(Partition)"<<std::endl;
}else
{
std::cout<<"(No partition)"<<std::endl;
} //从一个序列中划分出符合谓词条件的2部分(true/false);
std::array<int,> sort_array1,sort_array2;
std::partition_copy(sort_vec.begin(),sort_vec.end(),sort_array1.begin(),sort_array2.begin(),[](int& value){return value%==;}); for(auto& iter:sort_array1)std::cout<<iter<<"\t";
std::cout<<std::endl;
for(auto& iter:sort_array2)std::cout<<iter<<"\t"; // 正常排序 std::array<int,> sort_array{,,,,,,,,,,,};
//排序算法
//std::stable_sort 稳定排序
std::sort(sort_array.begin(),sort_array.end());
//std::sort(sort_array.begin(),sort_array.end(),[](int& a,int& b){return a>=b;}); if(std::is_sorted(sort_array.begin(),sort_array.end()))
{
std::cout<<"sort"<<std::endl;
}else
{
std::cout<<"not sort"<<std::endl;
} //快速排序
std::array<int,> sort_array{,,,,,,,,,,,}; std::nth_element(sort_array.begin(),sort_array.begin()+,sort_array.end()); for(auto& iter:sort_array)std::cout<<iter<<"\t"; //重排序列算法 std::array<int,> sort_array{,,,,,,,,,,,};
//删除符合条件的,用保留的元素覆盖删除元素
std::remove(sort_array.begin(),sort_array.end(),);
std::remove_if(sort_array.begin(),sort_array.end(),[](int& value){return value%==;});
//std::remove_copy(sort_array.begin(),sort_array.end(),dest,value);
//std::remove_copy_if(sort_array.begin(),sort_array.end(),dest,comp); //序列相邻重复元素删除,通过覆盖删除
//std::unique(beg,end);
//std::unique(beg,end,binaryPred);
//std::unique_copy(beg,end,dest)
//std::unique_copy_if(beg,end,dest,binaryPred); for(auto& iter:sort_array)std::cout<<iter<<"\t"; //截取序列,从某个元素到end之间的范围 截取到beg之前
std::vector<int> vec18{,,,,,,,,}; std::rotate(vec18.begin(),vec18.begin()+,vec18.end());
//std::rotate_copy(beg,mid,end,dest); for(auto& iter:vec18)std::cout<<iter<<"\t"; //翻转序列
//std::reverse(beg,end);
//std::reverse(beg,end,dest); //随机序列
std::vector<int> random_vec{, , , , , , , }; unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
// std::random_shuffle(random_vec.begin(),random_vec.end());
//std::random_shuffle(random_vec.begin(),random_vec.end(),[](int seed){return std::rand()%seed;});
std::shuffle(random_vec.begin(), random_vec.end(), std::default_random_engine(seed)); for (auto &iter:random_vec)std::cout << iter << "\t"; //排列算法 std::vector<int> vec1{, , , , , };
std::vector<int> vec2{, , , , , }; //2序列是否是1序列的排列,是返回true;
//可以添加bianryPred选择排列方式
if (std::is_permutation(vec1.begin(), vec1.end(), vec2.begin()))
std::cout << "vec2 is_permutation belong vec1" << std::endl; //1 2 3
//1 3 2
//2 1 3
//2 3 1
//3 1 2
//3 2 1
//根据字典序列排序,默认降序排序,比如(1 2 3) ,首元素排列中1最小且第二个元素2小于排列(1 3 2),所以排列(1 2 3)在第一个,其他依次类推;
std::vector<int> vec_permutation{,,};
do
{
std::cout<<vec_permutation[]<<" "<<vec_permutation[]<<" "<<vec_permutation[]<<std::endl; //可以添加bianryPred选择排列方式
//std::pre_permutation为序列反向
}while(std::next_permutation(vec_permutation.begin(),vec_permutation.end())); //最后容器元素被置为靠头第一个排列 //字典序列比较
std::lexicographical_compare(beg,end,beg2,end2);
std::lexicographical_compare(beg,end,beg2,end2,comp); //容器1是否包含在容器2的元素
int container[] = {,,,,,,,,,};
int continent[] = {,,,}; std::sort (container,container+);
std::sort (continent,continent+); // using default comparison:
if ( std::includes(container,container+,continent,continent+) )
std::cout << "container includes continent!\n"; // using myfunction as comp:
if ( std::includes(container,container+,continent,continent+, [](int& a,int& b){ return a<b; }) )
std::cout << "container includes continent!\n"; std::set_union 把2有序的序列合并成一个有序序列到第三个容器中
int first[] = {,,,,};
int second[] = {,,,,};
std::vector<int> v(); // 0 0 0 0 0 0 0 0 0 0
std::vector<int>::iterator it; std::sort (first,first+); // 5 10 15 20 25
std::sort (second,second+); // 10 20 30 40 50 it=std::set_union (first, first+, second, second+, v.begin());
// 5 10 15 20 25 30 40 50 0 0
v.resize(it-v.begin()); // 5 10 15 20 25 30 40 50 std::cout << "The union has " << (v.size()) << " elements:\n";
for (it=v.begin(); it!=v.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n'; std::vector<int> int_vec1{,,,,,,,,};
std::vector<int> int_vec2{,,};
std::vector<int> int_vec;
int_vec.resize();
//寻找交集
std::set_intersection(int_vec1.begin(),int_vec1.end(),int_vec2.begin(),int_vec2.end(),int_vec.begin());
for(auto& iter:int_vec)std::cout<<iter<<std::endl; 出现在第一个序列,但不出现在第二个序列 std::vector<int> int_vec1{, , , , , , , , };
std::vector<int> int_vec2{, , };
std::vector<int> int_vec(); std::set_difference(int_vec1.begin(),int_vec1.end(),int_vec2.begin(),int_vec2.end(),int_vec.begin()); for(auto& iter:int_vec)std::cout<<iter<<std::endl; //非交集区域 std::vector<int> int_vec1{, , , , , , , , };
std::vector<int> int_vec2{, , };
std::vector<int> int_vec(); auto end=std::set_symmetric_difference(int_vec1.begin(),int_vec1.end(),int_vec2.begin(),int_vec2.end(),int_vec.begin());
int_vec.resize(end-int_vec.begin()); for(auto& iter:int_vec)std::cout<<iter<<std::endl; //前两个应用值比较或者初始化链表值比较 std::initializer_list<int> li{,,,,,,,}; std::cout<<std::max(,)<<std::endl;
std::cout<<std::min(,)<<std::endl;
std::cout<<std::min(li)<<std::endl;
std::cout<<std::max(li)<<std::endl;
auto pair=std::minmax(li);
std::cout<<pair.first<<" "<<pair.second<<std::endl; std::vector<int> in_vec{,,,,,,}; std::cout<<*std::min_element(in_vec.begin(),in_vec.end())<<std::endl;
std::cout<<*std::max_element(in_vec.begin(),in_vec.end())<<std::endl;
auto pair_=std::minmax_element(in_vec.begin(),in_vec.end());
std::cout<<*pair_.first<<" "<<*pair_.second<<std::endl; //数值算法 //容器累和,sum代表初值
int array[]={,,,,,,};
int sum=;
std::cout<<std::accumulate(array,array+,sum)<<std::endl;
std::cout<<std::accumulate(array,array+,sum,[](int& a,int& b){ return a+b;})<<std::endl;
std::cout<<sum<<std::endl; //容器元素对应想乘求和
int myaccumulator (int x, int y) {return x-y;}
int myproduct (int x, int y) {return x+y;} int init = ;
int series1[] = {,,};
int series2[] = {,,}; std::cout << "using default inner_product: ";
std::cout << std::inner_product(series1,series1+,series2,init);
std::cout << '\n'; std::cout << "using functional operations: ";
std::cout << std::inner_product(series1,series1+,series2,init,
std::minus<int>(),std::divides<int>());
std::cout << '\n'; std::cout << "using custom functions: ";
std::cout << std::inner_product(series1,series1+,series2,init,
myaccumulator,myproduct);
std::cout << '\n'; //序列当前位置到beg范围求和.当前位置递增
int array[]={,,,,,,};
int result[]{};
std::partial_sum(array,array+,result);
for(auto& iter:result)std::cout<<iter<<"\t";
std::cout<<std::endl; //当前位置减去前一个元素
int array[] = {, , , , , , };
int result[]{};
std::adjacent_difference(array, array + , result);
for (auto &iter:result)std::cout << iter << "\t";
std::cout << std::endl; //填充容器给定数值递增
int array[];
std::iota(array,array+,);
for(auto& iter:array)std::cout<<iter<<"\t";
std::cout<<std::endl; return ;
}

c++中的 Stl 算法(很乱别看)的更多相关文章

  1. STL笔记(6)标准库:标准库中的排序算法

    STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...

  2. 论C++STL源代码中关于堆算法的那些事

    关于堆,我们肯定熟知的就是它排序的时间复杂度在几个排序算法里面算是比較靠上的O(nlogn)常常会拿来和高速排序和归并排序讨论,并且它还有个长处是它的空间复杂度为O(1), 可是STL中没有给我们提供 ...

  3. STL中的查找算法

    STL中有很多算法,这些算法可以用到一个或多个STL容器(因为STL的一个设计思想是将算法和容器进行分离),也可以用到非容器序列比如数组中.众多算法中,查找算法是应用最为普遍的一类. 单个元素查找 1 ...

  4. STL中的所有算法(70个)

    STL中的所有算法(70个)----9种类型(略有修改by crazyhacking) 参考自: http://www.cppblog.com/mzty/archive/2007/03/14/1981 ...

  5. STL 算法中函数对象和谓词

    STL 算法中函数对象和谓词 函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特 ...

  6. STL中的排序算法

    本文转自:STL中的排序算法 1. 所有STL sort算法函数的名字列表: 函数名    功能描述 sort   对给定区间所有元素进行排序 stable_sort 对给定区间所有元素进行稳定排序 ...

  7. 实战c++中的vector系列--vector的遍历(stl算法、vector迭代器(不要在循环中推断不等于end())、operator[])

    遍历一个vector容器有非常多种方法.使用起来也是仁者见仁. 通过索引遍历: for (i = 0; i<v.size(); i++) { cout << v[i] << ...

  8. 从Hadder看蛋白质分子中的加氢算法

    技术背景 PDB(Protein Data Bank)是一种最常用于存储蛋白质结构的文件.而我们在研究蛋白质构象时,往往更多的是考虑其骨架,因此在很多pdb文件中直接去掉了氢原子.但是在我们构建蛋白质 ...

  9. STL算法中函数对象和谓词

    函数对象和谓词定义 函数对象: 重载函数调用操作符的类,其对象常称为函数对象(function object),即它们是行为类似函数的对象.一个类对象,表现出一个函数的特征,就是通过“对象名+(参数列 ...

随机推荐

  1. python绘制三维图

    作者:桂. 时间:2017-04-27  23:24:55 链接:http://www.cnblogs.com/xingshansi/p/6777945.html 本文仅仅梳理最基本的绘图方法. 一. ...

  2. vue-cli 3.0 图片路径问题(何时使用 public 文件夹)

    1. 图片放入public文件夹下时 参考:https://cli.vuejs.org/zh/guide/html-and-static-assets.html#public-%E6%96%87%E4 ...

  3. 利用fiddler core api 拦截修改 websocket 数据

    一般的中间人攻击基本都是拦截修改普通的http协议里面的内容,而对于怎么拦截修改websocket协议传输的内容好像都没有多少介绍. talk is cheap show me the code us ...

  4. Asp.Net_Wcf跟Wpf的区别

    摘要:WCF,你就先把它想成WebService的下一代也没什么问题.WCF为WindowsCommunicationFoundation,是Microsoft为构建面向服务的应用提供的分布式通信编程 ...

  5. FME Cloud 账号申请流程

    第一步,访问SAFE的FME Cloud注册页,官网明确表态,如果你是一个新的FME Cloud用户,你可以免费获得一个初级版.地址:https://console.fmecloud.safe.com ...

  6. Scrapy框架中的CrawlSpider

    小思考:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法二: ...

  7. Unity 图文重现官方教程视频 2droguelike 第一集

    初衷: 本人初学Unity,四处收集了一些视频和教材,学习和摸索了一段时间, 我发现官网教程简单易上手,只不过他是英文讲解不方便,我就想把他翻译翻译吧, 然后我又发现看视频学习要暂停回放好多遍,麻烦, ...

  8. Python进阶量化交易场外篇4——寻找最优化策略参数

    新年伊始,很荣幸笔者的<教你用 Python 进阶量化交易>专栏在慕课专栏板块上线了,欢迎大家订阅!为了能够提供给大家更轻松的学习过程,笔者在专栏内容之外会陆续推出一些手记来辅助同学们学习 ...

  9. PAT甲题题解-1095. Cars on Campus(30)-(map+树状数组,或者模拟)

    题意:给出n个车辆进出校园的记录,以及k个时间点,让你回答每个时间点校园内的车辆数,最后输出在校园内停留的总时间最长的车牌号和停留时间,如果不止一个,车牌号按字典序输出. 几个注意点: 1.如果一个车 ...

  10. week2--操作系统是如何工作的

    潘恒   原创作品转载请注明出处   <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.一个简单的时间 ...