generic.cc

/**
   @file      generic.cc
   @author    Mitch Richling <http://www.mitchr.me/>
   @Copyright Copyright 2000 by Mitch Richling.  All rights reserved.
   @brief     @EOL
   @Keywords  Standard C++ STL algorithm
   @Std       C++98

              This program is an example of how write some a simple
              generic function to work with things in the STL.
*/

#include <vector>               /* STL vector      C++   */ 
#include <list>                 /* STL list        C++   */
#include <iostream>             /* C++ iostream    C++   */
#include <algorithm>            /* STL algorithm   C++   */

template <class T> void printSeq(char *msg, T b, T e);

int main() {
  std::vector<int> demoVec;
  std::list<int> demoLst;

  // First we put something in our vector and our list..
  for(int i=1; i<10; i++) {
    demoVec.push_back(i);
    demoLst.push_back(i);
  }

  // Now call our printSeq function (note we don't need to specify the template
  // parameter in this case..
  printSeq("vec: ", demoVec.begin(), demoVec.end());
  printSeq("lst: ", demoLst.begin(), demoLst.end());

  // While we are not required to specify the template parameter, we can.  If we
  // did, it would look something like this:
  // printSeq<std::vector<int>::iterator >("vec: ", demoVec.begin(), demoVec.end());
  // printSeq<std::list<int>::iterator   >("lst: ", demoLst.begin(), demoLst.end());
}

template <class T> void printSeq(char *msg, T b, T e) {
     std::cout << msg;
     for(T vi = b; vi != e; ++vi)
       std::cout << *vi << " ";
     std::cout << std::endl;  
}



Generated by GNU Enscript 1.6.5.2.