本文为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的更多相关文章

  1. STL 源代码剖析 算法 stl_algo.h -- search

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie search --------------------------------------- ...

  2. STL 源代码剖析 算法 stl_algo.h -- partial_sort / partial_sort_copy

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partial_sort / partial_sort_copy ------------- ...

  3. STL 源代码剖析 算法 stl_algo.h -- lower_bound

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie lower_bound(应用于有序区间) ------------------------- ...

  4. STL 源代码剖析 算法 stl_algo.h -- random_shuffle

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie random_shuffle ------------------------------- ...

  5. STL 源代码剖析 算法 stl_algo.h -- merge sort

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie merge sort ----------------------------------- ...

  6. STL 源代码剖析 算法 stl_algo.h -- partition

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie partition ------------------------------------ ...

  7. STL 源代码剖析 算法 stl_algo.h -- equal_range

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie equal_range(应用于有序区间) ------------------------- ...

  8. STL 源代码剖析 算法 stl_algo.h -- inplace_merge

    本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie inplace_merge(应用于有序区间) ----------------------- ...

  9. STL 源代码剖析 算法 stl_algo.h -- next_permutation

    本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie next_permutation ----------------------------- ...

随机推荐

  1. 如何做一个像btbook.net这样的搜片神器?

    这几天btbook.net这个搜片神器网站火了, 让我这个无工作的人, 也想做一个出来, 不然时间不好打发, 本人的草稿站: fastbot.me (刚发布几个小时, 体验等几天再做) 现在说说这种搜 ...

  2. WebForm中使用MVC

    http://www.cnblogs.com/encoding/articles/3556046.html ********************************************** ...

  3. R ggplot2 线性回归

    摘自  http://f.dataguru.cn/thread-278300-1-1.html library(ggplot2) x=1:10y=rnorm(10)a=data.frame(x= x, ...

  4. SpringMVC 多视图解析器配置以及问题

    在SpringMVC模式当中可以通过如下配置来支持多视图解析 <!-- jsp jstl --> <bean id="JSPViewResolver" class ...

  5. 路由表及route使用

    路由表 linux下通过route可以查看本地路由表: Kernel IP routing table Destination     Gateway         Genmask         ...

  6. Yarn NodeManager restart

    一.介绍默认Yarn NodeManager重启后会断开所有当前正在运行的container的状态,这意味着重启后需要重新启动container进程,该特性的作用就是把NM的状态临时保存到本地,重启后 ...

  7. kill 的常用信号

    kill命令用于终止指定的进程(terminate a process),是Unix/Linux下进程管理的常用命令.通常,我们在需要终止某个或某些进程时,先使用ps/pidof/pstree/top ...

  8. QFTPERROR lists

  9. skynet1.0阅读笔记2_skynet的消息投递skynet.call

    为了了解 skynet.call 的调用过程,需要先看看 skynet的队列是如何把包分到不同工作线程的.看下图 查看 global_queue 的skynet_globalmq_push和skyne ...

  10. Android修改默认SharedPreferences文件的路径,SharedPreferences常用工具类

    import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; ...