C++ Tutorial/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

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