STL 源代码剖析 算法 stl_algo.h -- search
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
search
-------------------------------------------------------------------------
描写叙述:在序列一[first1, last1) 所涵盖的区间中。查找序列二[first2, last2) 的首次出现点。
思路:
1.遍历序列二
2.假设两序列的当前元素一样,都前进 1
3.否则序列二的迭代器又一次指向開始元素,序列一前进 1 ,序列一的长度减 1
复杂度:
最坏情况是平方: 最多 (last1 - first1) * (last2 - first2) 次比較。 但最坏情况非常少出现。
平均情况下是线性复杂度
源代码:
template <class ForwardIterator1, class ForwardIterator2>
inline ForwardIterator1 search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2)
{
return __search(first1, last1, first2, last2, distance_type(first1),
distance_type(first2));
} //没看源代码之前,还以为会有什么复杂的算法。结果也仅仅是遍历
//假设没有假设(比方有序什么的),STL里的很多算法实现也是挺普通的做法
template <class ForwardIterator1, class ForwardIterator2, class Distance1,
class Distance2>
ForwardIterator1 __search(ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
Distance1*, Distance2*) {
Distance1 d1 = 0;
distance(first1, last1, d1);
Distance2 d2 = 0;
distance(first2, last2, d2); if (d1 < d2) return last1; //假设第二序列大于第一序列,不可能成为其子序列 ForwardIterator1 current1 = first1;
ForwardIterator2 current2 = first2; while (current2 != last2) // 我的第一感觉是遍历第一序列。结果人家是遍历第二序列。只是感觉代码写起来应该差点儿相同
if (*current1 == *current2) { // 假设这个元素同样。调整,以便比对下一个元素
++current1;
++current2;
}
else { //假设这个元素不同
if (d1 == d2) //假设两序列一样长了。就不可能成功了
return last1;
else { //假设两序列不一样长,调整序列标兵
current1 = ++first1;
current2 = first2;
--d1; //已经排序了序列一的一个元素。所以序列一的长度要减 1
}
}
return first1;
}
演示样例:
const char S1[] = "Hello, world!";
const char S2[] = "world";
const int N1 = sizeof(S1) - 1;
const int N2 = sizeof(S2) - 1; const char* p = search(S1, S1 + N1, S2, S2 + N2);
printf("Found subsequence \"%s\" at character %d of sequence \"%s\".\n",
S2, p - S1, S1);
STL 源代码剖析 算法 stl_algo.h -- 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 ----------------------------- ...
- STL 源代码剖析 算法 stl_algo.h -- nth_element
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie nth_element ---------------------------------- ...
随机推荐
- 多任务-进程之PID
1.进程pid,如何在程序中获取我们的进程号,从而查看当前的进程 # -*- coding:utf-8 -*- from multiprocessing import Process import o ...
- Vue export和import
config/index.js export default '123456'; import strs from '@/config'; //此处直接写@config就可以, 如果是export ...
- textarea统计字数
开发项目中经常会用到,textarea统计字数 源码如下: <!DOCTYPE html><html lang="en"><head> < ...
- 不安装Oracle客户端,用plsql连接远程Oracle数据库(绝对解决你的问题)
1,首先准备下载两个软件,一个是instantclient.zip,另一个是plsql安装包.但是得确定您的电脑是32位还是64位,我这边提供了32位和64位的供您下载: 百度网盘:https://p ...
- Android干货大放送:书籍、教程、工具各种全
最全干货分享,本文收集整理了Android开发所需的书籍.教程.工具.资讯和周刊各种资源,它们能让你在Android开发之旅的各个阶段都受益. 入门 <Learning Android(中文版) ...
- SCN 时间戳的相互转换
SQL> select * from v$version where rownum=1; BANNER --------------------------------------------- ...
- [Typescript] Build Method decorators in Typescript
To using decorate, we can modifiy tsconfig.json: { "compilerOptions": { ... "experime ...
- eclipse:报错信息The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path
JavaWeb: 报错信息The superclass "javax.servlet.http.HttpServlet" was not found on the Java Bui ...
- jni传递对象中包含arraylist对象。
相信在使用jni的过程中,总是要传递各种各样的类型,在这其中,我也碰到了一些问题. 简单的传一些内容,相信在网上一搜一大把. 所以我们就来说说.传递对象中包含arraylist吧. 在这里先给大家一个 ...
- zzulioj--1807--小明在努力(递归)
1807: 小明在努力 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 95 Solved: 35 SubmitStatusWeb Board Des ...