C++/Deque/deque find

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Illustrating the generic search algorithm: Search for first occurrence of deque"s contents as a subsequence of the vector contents

  
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <deque>
using namespace std;
int main()
{
  vector<int> vector1(20);
  deque<int>  deque1(5);
  int i;
  for (i = 0; i < 20; ++i) 
    vector1[i] = i;
  for (i = 0; i < 5; ++i) 
    deque1[i] = i + 5;

  vector<int>::iterator k = search(vector1.begin(), vector1.end(), deque1.begin(), deque1.end());
  for (i = 0; i < 5; ++i) 
    cout <<  *(k + i);
  return 0;
}
/* 
56789
 */


Search across two deques

  
#include <iostream>
#include <deque>
#include <algorithm>
#include <string>
using namespace std;
int main()
{
  deque<string> log;
  deque<string> break_in;
  deque<string>::iterator itr;
  break_in.push_back("A");
  break_in.push_back("B");
  break_in.push_back("C");
  break_in.push_back("D");
  break_in.push_back("E");
  log.push_back("q");
  log.push_back("w");
  log.push_back("e");
  log.push_back("r");
  log.push_back("t");
  log.push_back("y");
  log.push_back("u");
  for(itr = log.begin(); itr != log.end(); ++itr)
    cout << *itr << endl;
  itr = search(log.begin(), log.end(), break_in.begin(), break_in.end());
  if(itr != log.end())
    cout << endl << "Possible attempted break-in found." << endl;
  else
    cout << endl << "No repeated password failures found." << endl;
  return 0;
}


Use find the search an element in deque

  
 
#include <iostream>
#include <cassert>
#include <deque>
#include <algorithm>  // For find
using namespace std;

int main()
{
  int x[5] = {1, 2, 3, 4, 5};
  deque<int> deque1(&x[0], &x[5]);
  // Search for the first occurrence
  deque<int>::iterator where = find(deque1.begin(), deque1.end(), 1);
  cout << *where << endl;
  return 0;
}
/* 
1
 */