STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation
1find查找
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
voidmain()
{
vector<int>myv;
myv.push_back(1);
myv.push_back(2);
myv.push_back(3);
myv.push_back(11);
myv.push_back(22);
myv.push_back(33);
for_each(myv.begin(),myv.end(), [](intv){cout <<v <<endl; });
auto i =find(myv.begin(),myv.end(), 23);
//说明已经到了最后面
if (i == myv.end())
{
std::cout << "23玩失踪";
}
else
{
//如果找到了则输出结果
std::cout << *i;
}
cin.get();
}
运行结果:
2find_if,find_if_not
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
voidmain()
{
vector<int>myv;
myv.push_back(1);
myv.push_back(2);
myv.push_back(3);
myv.push_back(11);
myv.push_back(22);
myv.push_back(33);
for_each(myv.begin(),myv.end(), [](intv){cout <<v <<endl; });
std::cout << "-------" <<std::endl;
//找到第一个比11大的值
autoii =find_if(myv.begin(),myv.end(), [](intv)->bool{return (v > 11); });
//找到第一个不比4大的值
auto i =find_if_not(myv.begin(),myv.end(), [](intv)->bool{return (v > 4); });
if (ii == myv.end())
{
std::cout << "没找到" << endl;
}
else
{
std::cout << *ii <<endl;
}
std::cout << "-------" <<std::endl;
if (i == myv.end())
{
std::cout << "玩失踪";
}
else
{
std::cout << *i;
}
cin.get();
}
运行结果是:
3sort,fill,for_each
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
template <classT>
classshow
{
public:
void operator()(T &t)
{
cout <<t <<" ";
}
};
voidmain()
{
list<int>list1;
vector<int>v1;
list1.push_back(121);
list1.push_back(12);
list1.push_back(122);
list1.push_back(23);
v1.push_back(121);
v1.push_back(12);
v1.push_back(122);
v1.push_back(17);
v1.push_back(23);
//list不能通过sort进行排序,如果放开这一句将出现错误
//sort(list1.begin(), list1.end());
//排序,简单的排序
sort(v1.begin(),v1.end());
//算法依赖于数据结构(链式,线性),不同的数据结构,算法不一样
//填充,指定位置数据进行初始化,将begin()+2到最后的数值填充为3
fill(v1.begin() + 3, v1.end(), 3);
for_each(list1.begin(),list1.end(),show<int>());
cout <<"\n";
for_each(v1.begin(),v1.end(),show<int>());
cin.get();
}
运行结果:
4.count,统计某个节点值相同的元素有多少个
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
voidmain()
{
multiset<int > myset;
myset.insert(3);
myset.insert(1);
myset.insert(2);
myset.insert(1);
myset.insert(2);
myset.insert(1);
myset.insert(2);
myset.insert(1);
myset.insert(1);
myset.insert(1);
cout <<"统计总共的节点" << endl;
inti = 0;
for (autoib =myset.begin(),ie =myset.end();ib !=ie;ib++,i++)
{}
cout <<i <<endl;
//统计有多少个节点1
cout <<"统计值为1的节点个数有多少个" << endl;
intnum =count(myset.begin(),myset.end(), 1);
cout <<num <<endl;
cin.get();
}
运行结果:
5. adjacent_find,依次打印出结果:
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
template <classT>
classshow
{
public:
void operator ()(T &t)
{
cout <<t <<" ";
}
};
voidmain()
{
multiset<int > myset;
myset.insert(3);
myset.insert(1);
myset.insert(2);
myset.insert(1);
myset.insert(2);
myset.insert(1);
myset.insert(2);
for_each(myset.begin(),myset.end(),show<constint>());
//通过这种方式一个个的打印出结果
autoit =adjacent_find(myset.begin(),myset.end());
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
//查找相同的数据,可以自己确定位置
it =adjacent_find(it,myset.end());
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
it++;
cout <<"\n" << *it <<endl;
cin.get();
}
运行结果:
6. random_shuffle实现随机排序
#include<iostream>
#include<vector>
#include<algorithm>
#include<list>
#include<set>
#include<string>
usingnamespacestd;
template <classT>
classshow
{
public:
void operator ()(T &t)
{
cout <<t <<" ";
}
};
voidmain()
{
vector<int>v1;
for (inti = 0;i < 10;i++)
{
v1.push_back(i);
}
for_each(v1.begin(),v1.end(),show<int>());
cout <<"\n";
//通过random_shuffle的方式实现随机排序vector
random_shuffle(v1.begin(),v1.end());
for_each(v1.begin(),v1.end(),show<int>());
random_shuffle(v1.begin(),v1.end());
for_each(v1.begin(),v1.end(),show<int>());
random_shuffle(v1.begin(),v1.end());
for_each(v1.begin(),v1.end(),show<int>());
for_each(v1.begin(),v1.end(),show<int>());
partition(v1.begin(),v1.end(),isok);
for_each(v1.begin(),v1.end(),show<int>());
cout <<a[0] <<" " <<a[1] <<" " <<a[2] <<" " <<a[3] <<"\n";
} while (prev_permutation(a,a+4));
for_each(myvector.begin(),myvector.end(),show<char>());
//sort(one.begin(), one.begin() + 3);
sort(myvector.begin(),myvector.begin() + 3);
for_each(myvector.begin(),myvector.end(),show<char>());
student(stringstr,intnum) :name(str),score(num)
bool operator <(conststudent &s1)const
partial_sort(ss.begin(),ss.begin() + 4, ss.end());//部分排序
std::cout << ss[i].name << ss[i].score << "\n";
STL算法find,find_if,find_if_not,sort,fill,for_each,count,adjacent_find,random_shuffle,prev_permutation相关推荐
- STL算法algorithm,
2019独角兽企业重金招聘Python工程师标准>>> STL算法部分主要由头文件<algorithm>,<numeric>,<functional&g ...
- STL算法学习[转]
原文:http://www.cppblog.com/mzty/archive/2007/03/14/19819.html STL算法学习,小结如下: 前提: 下载stl源码: http://www. ...
- 【C++】C++11 STL算法(一):非修改序列操作(Non-modifying sequence operations)
目录 一.all_of.any_of.none_of: 1.官方说明 2.谓词 3.STL算法对谓词的说明 4.谓词的五种模式 5.all_of (C++ 11) 6.any_of (C++ 11) ...
- C++11后的STL算法
文章目录 一.函数对象 二.预定义的函数对象 三.算法函数 1.自己实现foreach算法 2.自己实现的findif算法 3.自己实现bsort算法 一.函数对象 STL提供了很多处理容器的函数模板 ...
- C++中的STL算法详解
1.STL算法详解 STL提供能在各种容器中通用的算法(大约有70种),如插入.删除.查找.排序等.算法就是函数模板,算法通过迭代器来操纵容器中的元素.许多算法操作的是容器上的一个区间(也可以是整个容 ...
- c++STL算法基础
STL算法基础 算法概述 STL中算法分类 查找算法(13个) adjacent_find binary_search count count_if equal_range find find_end ...
- STL::算法::常见算法
总述 定位 泛型编程(GP)走了一条与面向对象编程(OOP)完全不同的道路,各种容器类的设计与实现也没有走严格意义的继承.接口机制.在STL的设计与实现中,算法并非是容器类的成员函数,而是一种搭配迭代 ...
- 第3章 感受(一)——3.14. Hello STL 算法篇
[回到目录] 白话C++ 3.14. Hello STL 算法篇 前一小节,在输出成绩时,我们从students中得到每个学生的学号,然后,通过for循环,在一个list中查找符合"学号等于 ...
- C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)
C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...
最新文章
- 学习 JavaScript (四)核心概念:操作符
- 11、计算机图形学——几何(贝塞尔曲线与曲面)
- linux中yum命令详解
- 【转】排序算法复习(Java实现)(一): 插入,冒泡,选择,Shell,快速排序...
- 20200915 椭圆范数的三角不等式证明:sqrt(x^T*Q*x)
- PhpExcel 删除默认的Sheet
- [Django]SE项目回忆录(二)-注册/登录功能的实现及细节
- Markdown_LaTex_极限符号
- 获取指定日期所属年份的第一天日期或最后一天日期
- web 前端 如何分享到instagram_如何找到靠谱的Web培训机构?web前端培训机构哪个好?...
- JAVA毕业设计后勤管理系统计算机源码+lw文档+系统+调试部署+数据库
- 第三篇:DAMA数据管理知识体系
- 清华技术经理自学Python全栈的从业笔记,欢迎收藏
- java 获取身份证前两位对应省份
- canvas实现flybird小游戏
- 弹出框(bootStrap模态框、bootbox、dbailog)
- matlab2016 wavread,matlabwavread用法
- Uni-app 小程序 App 的广告变现之路:激励视频广告
- 网易企业邮箱:与POP3、WebMail相比,为什么Imap4更被接受
- 备战双十一,你清楚积分兑换系统运营吗
热门文章
- pandas基本数据处理
- Java 设计模式之模板方法模式
- JavaSE 基础面试题
- wxWidgets:剪贴板 wxWidgets 示例
- wxWidgets:调试
- boost::range_const_reverse_iterator相关的测试程序
- hana::detail::variadic::split_at用法的测试程序
- Boost:bind绑定和数据成员的测试程序
- VTK:小部件之ImageTracerWidgetInsideContour
- VTK:可视化算法之LOxGrid