#algostuff.h /* PRINT_ELEMENTS() * - prints optional C-string optcstr followed by * - all elements of the collection coll * - separated by spaces */ template <class T> inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="") { typename T::const_iterator pos; std::cout << optcstr; for (pos=coll.begin(); pos!=coll.end(); ++pos) { std::cout << *pos << ' '; } std::cout << std::endl; } /* INSERT_ELEMENTS (collection, first, last) * - fill values from first to last into the collection * - NOTE: NO half-open range */ template <class T> inline void INSERT_ELEMENTS (T& coll, int first, int last) { for (int i=first; i<=last; ++i) { coll.insert(coll.end(),i); } } //test.cpp #include <vector> #include <string> #include <iostream> #include "algostuff.h" using namespace std; int print(char c) { cout<< c << endl; } int main() { vector<char> v1(20); string str = "hello world"; copy(str.begin(), str.end(), v1.begin()); for_each(v1.begin(),v1.end(),print); char buf[32] = {0}; strcpy(buf, &v1[0]); cout << "len :" << strlen(buf) << endl; cout << buf << endl; vector<int> v2; INSERT_ELEMENTS(v2,1,10); int iBuf[10] = {0}; memcpy(iBuf, &v2[0], sizeof(int)*v2.size()); for(int i = 0; i < v2.size(); i++) { cout << iBuf[i] << endl; } vector<int> v3(iBuf,iBuf+10); PRINT_ELEMENTS(v3); return 0; }