Mega Code Archive

 
Categories / C++ Tutorial / Deque
 

Use insert iterator adaptors to insert one deque into another by way of the copy() algorithm

#include <iostream> #include <iterator> #include <deque> #include <string> using namespace std; void show(const char *msg, deque<string> dq); int main() {   deque<string> dq, dq2, dq3, dq4;   dq.push_back("A");   dq.push_back("B");   dq.push_back("C");   dq.push_back("D");   dq.push_back("E");   dq.push_back("F.");   dq2.push_back("G");   dq2.push_back("H");   dq2.push_back("I");   dq3.push_back("J");   dq3.push_back("K");   dq3.push_back("L");   dq4.push_back("M");   dq4.push_back("N");   dq4.push_back("O");   cout << dq.size() << endl;   show("Original contents of dq:", dq);   // Use an insert_iterator to insert dq2 into dq.   copy(dq2.begin(), dq2.end(), inserter(dq, dq.begin()+3));   cout << "Size of dq after inserting dq2: " << dq.size() << endl;   show("dq2:", dq);   // Use a back_insert_iterator to insert dq3 into dq.   copy(dq3.begin(), dq3.end(), back_inserter(dq));   cout << "Size of dq after inserting dq3: ";   cout << dq.size() << endl;   show("dq3:", dq);   // Use a front_insert_iterator to insert dq4 into dq.   copy(dq4.begin(), dq4.end(), front_inserter(dq));   cout << "Size of dq after inserting dq4: " << dq.size() << endl;   show("dq4:", dq);   return 0; } void show(const char *msg, deque<string> dq) {   cout << msg << endl;   for(unsigned i=0; i < dq.size(); ++i)     cout << dq[i] << endl; }