C++ examples for STL Algorithm:copy
Using insert iterator adapters to insert one deque into another via the copy() algorithm.
#include <iostream> #include <iterator> #include <deque> #include <string> using namespace std; void show(const char *msg, deque<string> dq); int main()// ww w . j a v a2 s . com { deque<string> dq, dq2, dq3, dq4; dq.push_back("Iterators"); dq.push_back("are"); dq.push_back("the"); dq.push_back("the"); dq.push_back("are"); dq.push_back("STL"); dq.push_back("together."); dq2.push_back("glue"); dq2.push_back("that"); dq2.push_back("holds"); dq3.push_back("At"); dq3.push_back("At"); dq3.push_back("At"); dq3.push_back("the"); dq3.push_back("end."); dq4.push_back("front."); dq4.push_back("the"); dq4.push_back("front."); dq4.push_back("front."); dq4.push_back("At"); dq4.push_back("front."); cout << "Original size of dq: " << dq.size() << endl; show("Original contents of dq:\n", dq); cout << endl; // 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: "; cout << dq.size() << endl; show("Contents of dq after inserting dq2:\n", dq); cout << endl; // 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("Contents of dq after inserting dq3:\n", dq); cout << endl; // 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: "; cout << dq.size() << endl; show("Contents of dq after inserting dq4:\n", dq); return 0; } // Display the contents of a deque<string>. void show(const char *msg, deque<string> dq) { cout << msg; for(unsigned i=0; i < dq.size(); ++i) cout << dq[i] << " "; cout << "\n"; }