C++ STL 之 常用算法
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std; // transform 将一个容器的元素 搬运 到另一个容器中
struct MyPlus
{
int operator()(int val)
{
return val + ;
}
}; void MyPrint(int val)
{
cout << val << " ";
} void test01()
{
vector<int> v1;
vector<int> v2;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
v2.resize(v1.size()); // 开辟空间
transform(v1.begin(), v1.end(), v2.begin(), MyPlus());
for_each(v2.begin(), v2.end(), MyPrint);
cout << endl;
cout << "--------------------" << endl;
} // 常用的查找算法
void test02()
{
vector<int> v1;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
vector<int>::iterator ret = find(v1.begin(), v1.end(), );
if (ret == v1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了:" << *ret << endl;
}
cout << "--------------------" << endl;
} class Person
{
public:
Person(int age, int id) : age(age), id(id){}
bool operator== (const Person& p)
{
return p.id == this->id && p.age == this->age;
}
public:
int id;
int age;
}; void test03()
{
vector<Person> v1;
Person p1(, ), p2(, );
v1.push_back(p1);
v1.push_back(p2);
vector<Person>::iterator ret = find(v1.begin(), v1.end(), p1);
if (ret == v1.end())
{
cout << "没有找到!" << endl;
}
else
{
cout << "找到了!" << endl;
}
cout << "--------------------" << endl;
} bool MySearch(int val)
{
return val > ;
} bool MySearch2(int val)
{
return val > ;
} void test04()
{
vector<int> v1;
for (int i = ; i < ; i++)
{
v1.push_back(i);
}
v1.push_back();
bool ret = binary_search(v1.begin(), v1.end(), );
if (ret)
{
cout << "找到了!" << endl;
}
else
{
cout << "没有找到!" << endl;
} vector<int>::iterator it = adjacent_find(v1.begin(), v1.end());
if (it != v1.end())
{
cout << "找到相邻重复元素:" << *it << endl;
}
else
{
cout << "没有找到相邻重复元素!" << endl;
} // find_f 会根据我们的条件(函数) ,返回第一个满足条件的元素的迭代器
it = find_if(v1.begin(), v1.end(), MySearch);
if (it != v1.end())
{
cout << "找到:" << *it << endl;
}
else
{
cout << "没有找到!" << endl;
} // count count_if
int num = count(v1.begin(), v1.end(), );
cout << "9出现的次数:" << num << endl;
num = count_if(v1.begin(), v1.end(), MySearch2);
cout << "大于5的个数:" << num << endl;
} int main()
{
test01();
test02();
test03();
test04();
getchar();
return ;
}
C++ STL 之 常用算法的更多相关文章
- STL中常用算法
一.排序 sort sort(first_pointer,first_pointer+n,cmp) 默认为升序 若要使用降序,自行写cmp 函数 bool cmp(int a,int b){ retu ...
- STL——配接器、常用算法使用
学习STL,必然会用到它里面的适配器和一些常用的算法.它们都是STL中的重要组成部分. 适配器 在STL里可以用一些容器适配得到适配器.例如其中的stack和queue就是由双端队列deque容器适配 ...
- [C++ STL] 常用算法总结
1 概述 STL算法部分主要由头文件<algorithm>,<numeric>,<functional>组成.要使用 STL中的算法函数必须包含头文件<alg ...
- ACM常用算法及练习(2)
ACM常用算法及练习 知识类型 重要度 容易度 应掌握度 典型题 其他 数据结构(5) 链表 ★★☆ ★★★ ★★☆ 栈 stack ★★★ ★★★ ★★★ HLoj120 ...
- ACM常用算法及练习(1)
ACM常用算法及练习 第一阶段:练经典常用算法,下面的每个算法给我打上十到二十遍,同时自己精简代码,因为太常用,所以要练到写时不用想,10-15分钟内打完,甚至关掉显示器都可以把程序打出来. 1.最短 ...
- 总结Objective-c常用算法
今天是星期天,想睡到10点起床,结果认为自己太奢侈了,不能这么做,于是把闹钟设置成了6:30:结果终于9:36醒了,起床,无缘无故迟了,好吧,就算太累了吧,周天就原谅自己一回.终于到了中午 ...
- Atitit 编程语言常用算法attilax总结
Atitit 编程语言常用算法attilax总结 1. 编译算法分类and 数据操作算法.1 1.1. Tab driver stat 状态转换表格算法1 1.2. Nest case 词法分析 ...
- GJM : 数据结构 - 轻松看懂机器学习十大常用算法 [转载]
转载请联系原文作者 需要获得授权,非法转载 原文作者将享受侵权诉讼 文/不会停的蜗牛(简书作者)原文链接:http://www.jianshu.com/p/55a67c12d3e9 通过本篇文章可以 ...
- C/C++常用算法【C语言顺序查找(随机数)】【1】
这是我学习唐峻,李淳的<C/C++常用算法第一天> 1.8.1. 查找数字: 程序随机生成一个拥有20个整数数据的数组,然后输入要查找的数据.接着,可以采用醉简单的逐个对比的方法进行查找, ...
随机推荐
- SQL 模糊查询条件的四种匹配模式
转: 执行数据库查询时,有完整查询和模糊查询之分. 一般模糊语句格式如下: SELECT 字段 FROM 表 WHERE 某字段 LIKE 条件; 其中,关于条件,SQL提供了四种匹配模式: 1.%: ...
- React——相关工具概述
Create a New React App Use an integrated toolchain for the best user and developer experience. This ...
- PYTHON指定国内PIP源
一.LINUX: vi ~/.pip/pip.conf 输入内容: [global]index-url = http://pypi.douban.com/simple/[install]trusted ...
- jQuery调用WCF
jQuery要调用WCF,首先要创建service.svc服务文件,这里边需要注意: [ServiceContract(Namespace = "")] [AspNetCompat ...
- 动态绑定easyui datagrid列名
根据实时数据在同一个DataGrid中显示不同字段,本身easyui并没有支持动态绑定列名,只有show属性显示或隐藏某字段.今天在网上看到直接修改easyui类库动态绑定列名的方法,废话不多说直接借 ...
- iOS-浅谈iOS中三种生成随机数方法
ios 有如下三种随机数方法:
- 【POJ - 2010】Moo University - Financial Aid(优先队列)
Moo University - Financial Aid Descriptions 奶牛大学:奶大招生,从C头奶牛中招收N(N为奇数)头.它们分别得分score_i,需要资助学费aid_i.希望新 ...
- 【转载】Globelmposter勒索病毒最新变种预警
近日,深信服安全团队观察到Globelmposter勒索病毒又出现最新变种,加密后缀有Ares666.Zeus666.Aphrodite666.Apollon666等,目前国内已有多家大型医院率先发现 ...
- 计数器+打卡+习惯+目标APP推荐
目录 一.计数器类APP推荐 1.1. Thing Counter - Google Play 上的应用 1.2. Counter - Apps on Google Play 1.3. Counter ...
- 乐字节Java变量与数据结构之二:Java常量与变量
大家好,小乐又来给大家讲述Java基础知识.上次说了乐字节Java变量与数据类型之一:Java编程规范,关键字与标识符,这次将接着往下说,谈谈Java常量与变量. 1.常量 1).定义 在程序执行的过 ...