Mega Code Archive

 
Categories / C++ / Function
 

Creating a custom algorithm based on template

#include <iostream> #include <vector> #include <list> #include <algorithm> using namespace std; template<class ForIter>   void times2(ForIter start, ForIter end) {   while(start != end) {     *start *= 2;     start++;   } } int main() {   int i;   vector<int> vectorObject;   for(i = 0; i <10; i++)       vectorObject.push_back(i);   cout << "Initial Contents of vectorObject: ";   for(i = 0; i <vectorObject.size(); i++)     cout << vectorObject[ i ] << " ";   cout << endl;   times2(vectorObject.begin(), vectorObject.end());   cout << "Contents of vectorObject doubled: ";   for(i = 0; i <vectorObject.size(); i++)     cout << vectorObject[ i ] << " ";   cout << endl;   list<float> lst;   list<float>::iterator p;   for(i = 0; i <5; i++)       lst.push_back((float)i*3.1416);   cout << "Initial Contents of lst: ";   for(p=lst.begin(); p!=lst.end(); p++)     cout << *p << " ";   cout << endl;   times2(lst.begin(), lst.end());   cout << "Contents of lst doubled: ";   for(p=lst.begin(); p!=lst.end(); p++)     cout << *p << " ";   cout << endl;   return 0; }