C++ examples for STL Algorithm:copy
Use copy() to copy elements from a list to a vector.
#include <iostream> #include <vector> #include <list> #include <algorithm> using namespace std; template<class T> void show(const char *msg, T cont); int main()/*from w ww . j ava2 s .c om*/ { list<char> lst; // Add elements to lst. char str[] = "Algorithms act on containers"; for(int i = 0; str[i]; i++) lst.push_back(str[i]); // Create a vector that initially contains 40 periods. vector<char> v(40, '.'); show("Contents of lst:\n", lst); show("Contents of v:\n", v); // Copy lst into v. copy(lst.begin(), lst.end(), v.begin()+5); // Display result. show("Contents of v after copy:\n", v); return 0; } template<class T> void show(const char *msg, T cont) { cout << msg; T::iterator itr; for(itr=cont.begin(); itr != cont.end(); ++itr) cout << *itr; cout << "\n\n"; }