STL 源代码剖析 算法 stl_algo.h -- search_n
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
search_n
----------------------------------------------------------------------------------------
描写叙述:在序列[first, last) 所涵盖的区间中,查找"连续 count 个符合条件之元素"所形成的子序列。
并返回迭代器 last
思路:
1.首先找出 value 第一次出现点
2.该出现点的后面是否连续出现 count - 1 个 value
3.假设是,找到了,假设不是,在当前元素后的区间又一次找 value 的出现点
图6-6k
template <class ForwardIterator, class Integer, class T>
ForwardIterator search_n(ForwardIterator first, ForwardIterator last,
Integer count, const T& value) {
if (count <= 0)
return first;
else {
first = find(first, last, value); // 首先找出 value 第一次出现点
while (first != last) { // 这里的条件写成 last - first < n 是不是好些?
Integer n = count - 1; // value 还应该出现 n 次
ForwardIterator i = first;
++i;
while (i != last && n != 0 && *i == value) {
++i;
--n;
}
if (n == 0) // 找到了
return first;
else // 没找到,又一次从 i 開始找
first = find(i, last, value);
}
return last;
}
}
演示样例:
bool eq_nosign(int x, int y) { return abs(x) == abs(y); }
void lookup(int* first, int* last, size_t count, int val) {
cout << "Searching for a sequence of "
<< count
<< " '" << val << "'"
<< (count != 1 ? "s: " : ": ");
int* result = search_n(first, last, count, val);
if (result == last)
cout << "Not found" << endl;
else
cout << "Index = " << result - first << endl;
}
void lookup_nosign(int* first, int* last, size_t count, int val) {
cout << "Searching for a (sign-insensitive) sequence of "
<< count
<< " '" << val << "'"
<< (count != 1 ? "s: " : ": ");
int* result = search_n(first, last, count, val, eq_nosign);
if (result == last)
cout << "Not found" << endl;
else
cout << "Index = " << result - first << endl;
}
int main() {
const int N = 10;
int A[N] = {1, 2, 1, 1, 3, -3, 1, 1, 1, 1};
lookup(A, A+N, 1, 4);
lookup(A, A+N, 0, 4);
lookup(A, A+N, 1, 1);
lookup(A, A+N, 2, 1);
lookup(A, A+N, 3, 1);
lookup(A, A+N, 4, 1);
lookup(A, A+N, 1, 3);
lookup(A, A+N, 2, 3);
lookup_nosign(A, A+N, 1, 3);
lookup_nosign(A, A+N, 2, 3);
}
/*
The output is
Searching for a sequence of 1 '4': Not found
Searching for a sequence of 0 '4's: Index = 0
Searching for a sequence of 1 '1': Index = 0
Searching for a sequence of 2 '1's: Index = 2
Searching for a sequence of 3 '1's: Index = 6
Searching for a sequence of 4 '1's: Index = 6
Searching for a sequence of 1 '3': Index = 4
Searching for a sequence of 2 '3's: Not found
Searching for a (sign-insensitive) sequence of 1 '3': Index = 4
Searching for a (sign-insensitive) sequence of 2 '3's: Index = 4
*/
STL 源代码剖析 算法 stl_algo.h -- search_n的更多相关文章
- STL 源代码剖析 算法 stl_algo.h -- search
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...
- STL 源代码剖析 算法 stl_algo.h -- lower_bound
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- random_shuffle
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- merge sort
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- partition
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...
- STL 源代码剖析 算法 stl_algo.h -- equal_range
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- inplace_merge
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- next_permutation
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...
随机推荐
- python pip 升级
首先安装python,在百度中搜索python,进入python官网.点击download,选择电脑对应的系统进行下载,此处以windows系统的python 3.5.1进行介绍,点击即可下载. ...
- WP模板
ContentControl有两个属性: // 摘要: // 获取或设置 System.Windows.Controls.ContentControl 依赖项属性的值. // // 返回结果: // ...
- mysql 加入远程用户
USE mysql; GRANT ALL PRIVILEGES ON *.* TO panjing @"%" IDENTIFIED BY "123456"; F ...
- android开发中用到的px、dp、sp
先介绍一下这几个单位: px : pixels(像素),相应屏幕上的实际像素点. dip :device independent pixels,与密度无关的像素,基于屏幕密度的抽象单位. 在每英寸16 ...
- java中volatile关键字的含义<转>
在java线程并发处理中,有一个关键字volatile的使用目前存在很大的混淆,以为使用这个关键字,在进行多线程并发处理的时候就可以万事大吉. Java语言是支持多线程的,为了解决线程并发的问题,在语 ...
- log4j日志pattern配置
c category的名称,可使用{n}限制输出的精度.例如:logger名为"a.b.c",%c{2}将输出"b.c". C 产生log事件的java完全限定 ...
- 【C】——指针与const限定符
const限定符和指针结合起来常见的情况有以下几种. const int *a; int const *a; 这两种写法是一样的,a是一个指向const int型的指针,a所指向的内存单元不可改写,所 ...
- ubuntu workbench
先安装环境 sudo apt-get install python-paramikosudo apt-get install python-pysqlite2 当然mysql要装好 还要装 sudo ...
- JavaScript 关键字快速匹配
来源: http://www.cnblogs.com/index-html/archive/2013/04/17/js_keyword_match.html http://www.etherdream ...
- [svn] 在线安装
SVN地址:http://subclipse.tigris.org/update_1.6.x/