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...
请按任意键继续. . .

转载于:https://www.cnblogs.com/herd/p/11005472.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值