| |
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 |
|