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

includes(应用于有序区间)

-------------------------------------------------------------





描写叙述:S1和S2都必须是有序集合。推断序列二 S2 是否"涵盖于"序列一 S1,即"S2的每个元素是否都出现于 S1中"

思路:

1.遍历两个区间。直到当中一个走完

2.假设序列二的元素小于序列一的元素。则在序列一中不可能有元素等于序列二的当前元素了,直接返回 false

3.假设序列一的元素小于序列二的元素。则序列一前进 1

4.假设两序列元素相当,都前进 1

5.当有一个序列走完时。推断序列二是否走完了。



复杂度:最多 2 * ((last1- first1) + (last2 - first2)) - 1次比較

源代码:

// S1 和 S2是递增序列。

以 operator < 运行比較操作
// 假设是 RandomAccessIterator 能够得到区间长度,假设区间1的长度小于区间2的能够返回 false 提前结束循环
// 只是这里是 InputIterator , 可能是由于这点对效率影响不大吧, stl里没有专门针对 RandomAccessIterator 的优化
template <class InputIterator1, class InputIterator2>
bool includes(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
while (first1 != last1 && first2 != last2) // 两个区间都尚未走完
if (*first2 < *first1) // 序列二的元素小于序列一的元素,返回 false
return false;
else if(*first1 < *first2) // 序列一的元素小于序列二的元素,序列一前进 1
++first1;
else //两序列元素相等,各自前进 1
++first1, ++first2; return first2 == last2; //有一个序列走完了,推断是序列二是否走完了,假设是,涵盖情况成立,否则,不成立
}

演示样例:

int A1[] = { 1, 2, 3, 4, 5, 6, 7 };
int A2[] = { 1, 4, 7 };
int A3[] = { 2, 7, 9 };
int A4[] = { 1, 1, 2, 3, 5, 8, 13, 21 };
int A5[] = { 1, 2, 13, 13 };
int A6[] = { 1, 1, 3, 21 }; const int N1 = sizeof(A1) / sizeof(int);
const int N2 = sizeof(A2) / sizeof(int);
const int N3 = sizeof(A3) / sizeof(int);
const int N4 = sizeof(A4) / sizeof(int);
const int N5 = sizeof(A5) / sizeof(int);
const int N6 = sizeof(A6) / sizeof(int); cout << "A2 contained in A1: "
<< (includes(A1, A1 + N1, A2, A2 + N2) ? "true" : "false") << endl; // true
cout << "A3 contained in A1: "
<< (includes(A1, A1 + N2, A3, A3 + N3) ? "true" : "false") << endl; // false
cout << "A5 contained in A4: "
<< (includes(A4, A4 + N4, A5, A5 + N5) ? "true" : "false") << endl; // false
cout << "A6 contained in A4: "
<< (includes(A4, A4 + N4, A6, A6 + N6) ? "true" : "false") << endl; // true

STL 源代码分析 算法 stl_algo.h -- includes的更多相关文章

  1. STL 源代码分析 算法 stl_algo.h -- merge

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

  2. STL 源代码分析 算法 stl_algo.h -- binary_search

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

  3. STL 源代码分析 算法 stl_algo.h -- pre_permutation

    本文senlie原版的,转载请保留此地址:http://blog.csdn.net/zhengsenlie pre_permutation ------------------------------ ...

  4. STL 源代码分析 算法 stl_heap.h

    本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie heap ----------------------------------------- ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Swift - 使用UI Dynamics给UIKit组件添加移动吸附行为

    UI Dynamics是UIKit的一个新组成部分,它向iOS中的视图提供了与物理学有关的功能和动画.可以让你向视图中引入力和物理属性,可以让你的视图弹跳,舞动,受重力影响等等. 下面通过样例,演示使 ...

  2. android开发隐藏了actionbar仍然短暂闪现的解决方法

    有时候我们在代码里隐藏了actionbar,在打开应用时,仍然短暂闪现下actionbar,用户体验很不好.   最简单的方法是 在AndroidManifest.xml中设置主题中配置不显示titl ...

  3. POJ 2031 prim

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...

  4. Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity

    Caused by: android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity ...

  5. CentOS 6.4 + 曙光DS200 IPSan组建FTP服务器

    CentOS 6.4 + 曙光DS200 IPSan组建FTP服务器 http://write.blog.csdn.net/postedit/10911105#本系列文章由ex_net(张建波)编写, ...

  6. css3 animation动画事件

    当使用css3时,会遇到利用@keyframes来定义动画事件,利用以下3个事件,能够捕捉当前元素的动画: AnimationEnd //动画结束时 AnimationStart  //动画開始 An ...

  7. View实现涂鸦、撤销以及重做功能

    import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import j ...

  8. QNX---Interrupt vector numbers(原创!!!)

    Interrupt intr Description 0 A clock that runs at the resolution set by ClockPeriod() 1 Keyboard 2 S ...

  9. android花屏效果的实现(ViewPager的基本使用)

    1.程序运行效果图 二.代码实现 1.main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/re ...

  10. Thinkphp入门 二 —空操作、空模块、模块分组、前置操作、后置操作、跨模块调用(46)

    原文:Thinkphp入门 二 -空操作.空模块.模块分组.前置操作.后置操作.跨模块调用(46) [空操作处理] 看下列图: 实际情况:我们的User控制器没有hello()这个方法 一个对象去访问 ...