C++ STL 已序区间查找算法
#include <iostream>
#include <algorithm>
#include <list>
#include <functional>
#include <vector>
using namespace std;
//已序区间查找
int main()
{
list<int> list1;
for (int k = 0; k < 10; k++)
{
list1.insert(list1.end(), k);
}
list<int>::iterator list_iter1;
for (list_iter1 = list1.begin(); list_iter1 != list1.end(); ++list_iter1)
{
cout << *list_iter1 << " ";
}
cout << endl;
list<int>::iterator list_iter2;
bool isFound = binary_search(list1.begin(), list1.end(), 5);
if (isFound)
{
cout << "found element..." << endl;
}
else
{
cout << "not found..." << endl;
}
cout << "---------------------------------" << endl;
vector<int> vec1;
for (int k=4;k<8;k++)
{
vec1.push_back(k);
}
//includes 查找不用连续,如果查找连续,用search
bool isFound2 = includes(list1.begin(), list1.end(), vec1.begin(), vec1.end());
if (isFound2)
{
cout << "found element..." << endl;
}
else
{
cout << "not found..." << endl;
}
cout << "---------------------------------------------------" << endl;
vec1.push_back(56);
bool isFound3 = includes(list1.begin(), list1.end(), vec1.begin(), vec1.end());
if (isFound3)
{
cout << "found element..." << endl;
}
else
{
cout << "not found..." << endl;
}
system("pause");
return 0;
}
===================================================
0 1 2 3 4 5 6 7 8 9
found element...
---------------------------------
found element...
---------------------------------------------------
not found...
请按任意键继续. . .

C++ STL 已序区间查找算法的更多相关文章
- STL学习笔记(已序区间算法)
针对已序区间执行的算法,执行前提是源区间必须在某个排序准则下已序. 搜寻元素(Searching) 1.检查某个元素是否存在 bool binary_search(ForwardIterator be ...
- STL_算法_查找算法(lower_bound、upper_bound、equal_range)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) //全部容器适用(O(log(n))) 已序区间查找算法 lower_bound() //找第一个符合的 ...
- STL_算法_查找算法(binary_search、includes)
C++ Primer 学习中.. . 简单记录下我的学习过程 (代码为主) 全部容器适用(O(log(n))) 已序区间查找算法 binary_search //二分查 ...
- C++ STL之查找算法
C++STL有好几种查找算法,但是他们的用法上有很多共同的地方: 1.除了binary_search的返回值是bool之外(查找的了返回true,否则返回false),其他所有的查找算法返回值都是一个 ...
- 从零开始学C++之STL(四):算法简介、7种算法分类
一.算法 算法是以函数模板的形式实现的.常用的算法涉及到比较.交换.查找.搜索.复制.修改.移除.反转.排序.合并等等. 算法并非容器类型的成员函数,而是一些全局函数,要与迭代器一起搭配使用. 算法的 ...
- STL_算法_查找算法(find、find_if)
C++ Primer 学习中. .. 简单记录下我的学习过程 (代码为主) find . find_if /**********************线性查找O(n) find(); find_if ...
- cb34a_c++_STL_算法_查找算法_(7)_lower_bound
cb34a_c++_STL_算法_查找算法_(7)_lower_bound//针对已序区间的查找算法,如set,multiset关联容器-自动排序lower_bound()--第一个可能的位置uppe ...
- cb33a_c++_STL_算法_查找算法_(6)binary_search_includes
cb33a_c++_STL_算法_查找算法_(6)binary_search_includes//针对已序区间的查找算法,如set,multiset关联容器-自动排序binary_search(b,e ...
- cb32a_c++_STL_算法_查找算法_(5)adjacent_find
cb32a_c++_STL_算法_查找算法_(5)adjacent_findadjacent_find(b,e),b,begin(),e,end()adjacent_find(b,e,p),p-par ...
随机推荐
- 我是怎么找到电子书的 - IT篇
转自于:http://my.oschina.net/0757/blog/375908#OSC_h2_8 IT-ebooks http://it-ebooks.info/ http://www.it-e ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)
2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...
- 【简易DFS/BFS+标记搜索次序的数组】zznu-2025 : 简单环路
2025 : 简单环路 时间限制:1 Sec 内存限制:128 MiB提交:145 答案正确:41 提交 状态 编辑 讨论区 题目描述 有一个N x M 大小的地图,地图中的每个单元包含一个大写字母. ...
- python 打印 str 字符串的实际内容 repr(str)
python 打印 str 字符串的实际内容 repr(str) s = 'aa' print(repr(s))
- Codeforces Round #551 (Div. 2) E. Serval and Snake (交互题)
人生第一次交互题ac! 其实比较水 容易发现如果查询的矩阵里面包含一个端点,得到的值是奇数:否则是偶数. 所以只要花2*n次查询每一行和每一列,找出其中查询答案为奇数的行和列,就表示这一行有一个端点. ...
- 工作 巧遇 sql 查询 一组数据中 最新的一条
SELECT * FROM rsl a, (SELECT CODE, max(time_key) time_key FROM rsl GROUP BY CODE ) b WHERE a. CODE = ...
- [ES2019] Use JavaScript ES2019 flatMap to Map and Filter an Array
ES2019 introduces the Array.prototype.flatMap method. In this lesson, we'll investigate a common use ...
- npm install、npm install --save与npm install --save-dev (转)
仅供学习参考,侵权删 以npm安装msbuild为例: npm install msbuild: 会把msbuild包安装到node_modules目录中 不会修改package.json 之后运行n ...
- mysql日期相减取小时
mysql日期相减取小时 TIMESTAMPDIFF(HOUR,a.StartTime,a.EndTime)
- 收藏加备用。ext.xfs文件系统 文件恢复
注意 当确认误删除文件后立刻使用各种方式阻止新数据在写入该分区了. 设置只读或umount都可以 ext2 3 4 文件系统 可以用 extundelete 这个工具来恢复. github地址: ht ...