Demonstrate a deque. : Deque « Data Structure « C++






Demonstrate a deque.

Demonstrate a deque.


#include <iostream>
#include <deque>
#include <cstring>
using namespace std;

int main()
{
  deque<char> dequeObject1;
  char str[] = "Using a deque.";
  int i;

  for(i = 0; str[i]; i++) {
    dequeObject1.push_front(str[i]);
    dequeObject1.push_back(str[i]);
  }

  cout << "Original dequeObject1:\n";
  for(i = 0; i <dequeObject1.size(); i++)
    cout << dequeObject1[i];
  cout << "\n\n";

  for(i = 0; i <strlen(str); i++) 
     dequeObject1.pop_front();
  cout << "dequeObject1 after popping front:\n";
  for(i = 0; i <dequeObject1.size(); i++)
    cout << dequeObject1[i];
  cout << "\n\n";

  deque<char> dequeObject2(dequeObject1); 
  cout << "dequeObject2 original contents:\n";
  for(i = 0; i <dequeObject2.size(); i++)
    cout << dequeObject2[i];
  cout << "\n\n";

  for(i = 0; i <dequeObject2.size(); i++)
     dequeObject2[i] = dequeObject2[i]+1;
  
  cout << "dequeObject2 transposed contents:\n";
  for(i = 0; i <dequeObject2.size(); i++)
    cout << dequeObject2[i];
  cout << "\n\n";

  deque<char>::iterator p = dequeObject1.begin();
  while(p != dequeObject1.end()) {
    if(*p == 'a') break;
    p++;
  }

  dequeObject1.insert(p, dequeObject2.begin(), dequeObject2.end());

  cout << "dequeObject1 after insertion:\n";
  for(i = 0; i <dequeObject1.size(); i++)
    cout << dequeObject1[i];
  cout << "\n\n";

  return 0;
}

           
       








Related examples in the same category

1.One way to reverse-copy a deque.One way to reverse-copy a deque.
2.Use reverse iterators.Use reverse iterators.
3.Reverse iterators and copy.Reverse iterators and copy.
4.Assigning deque objects.Assigning deque objects.
5.Demonstrate swap() for dequeDemonstrate swap() for deque
6.Demonstrate resize() for dequeDemonstrate resize() for deque
7.Iterator values may change.Iterator values may change.
8.Demonstrate raw storage iteratorsDemonstrate raw storage iterators
9.Deque: push, pop, sort, find, begin, end, insertDeque: push, pop, sort, find, begin, end, insert