zoukankan      html  css  js  c++  java
  • 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...
    请按任意键继续. . .

  • 相关阅读:
    shell 函数用法
    shell read变量的读入
    利用系统函数模拟实现nginx 系统脚本启动的特殊颜色专业效果
    shell重定向介绍及使用
    监控MySQL或Web服务是否正常
    centos 6.5下安装nmap工具及简单用法
    if条件简单语法
    shell 的条件表达式及逻辑操作符简单介绍
    mysql常见的错误代码
    Linux MySql 安装与配置(二进制包)
  • 原文地址:https://www.cnblogs.com/herd/p/11005472.html
Copyright © 2011-2022 走看看