   STL containers question
  how can you make a function accept either a std::list or an std::vector and work the same?i have 2 classes, one of them has a vector of some things, and anotherone has list of the same kind of things.both must be able to have such collections processed the same way, and duplicating that code would just suck, as that portion is being rewritten all the time.   
  Just make it a template function. Typically the standard library would take templated input and output iterators, but if it works for you, you can just take the container directly.   ;template < class Container >void process( Container &c ){  typedef typename Container::iterator IteratorType;  IteratorType i = c.begin();  while ( i != c.end() ) {    // do the stuff with (*i)    ++i;  }}or, better yet:template <class T>class Foo:  public std::unary_function<T, void>{public:    void operator()(const T &x) const { // do the stuff with x    }};...// c is some containerstd::transform( c.begin(), c.end(), Foo() );// or, wrapped as beforetemplate < class Container >void bar( Container &c ){  Foo foo;  std::transform( c.begin(), c.end(), foo );};Quote:Original post by ElPeque2how can you make a function accept either a std::list or an std::vector and work the same?The way that the standard library does.   ;  antheus, i did something like the first option you proposed, and i got this error (and more)Error1error C2898: 'void CRenderer::setSceneState2(Container &)' : member function templates cannot be virtualc:\documents and settings\usuario\mis documentos\visual studio 2005\projects\engine 3\engine\crenderer.h26from the error you can see what i was trying to do. im quite new to templates :?thanks to all of you :)   ;  here is the pseudo:class A  {virtual void whatever (list) = 0;virtual void whatever (vector) = 0;  }i got class B : public A{void whatever (list); //implementedvoid whatever (vector); //does exactly the same};but having that code duplicated is not nice so id like to make it only 1 function. what you suggest would not work in this case if i understood the error correctly.i choose your approach as it seemed to be the easyest one. ill try to understand the others now...   ;  What you can do is something like:struct Interface {  virtual void process_element(Element & e) = 0;  template <typename T>  void process_container(T & container) {    for (typename T::iterator itr = container.begin();    itr != container.end();    ++itr) { process_element(*itr);    }  }  virtual ~Interface() = 0;};;  yes, i was hoping not having to make so many calls to a virtual function, but i guess you can't have them all :?.thanks for your help :)   ;  To avoid overhead, you could try keeping the "duplicated" interface, and factoring out the common bits in the B *implementation*:struct A {  virtual void whatever(list&) = 0;  virtual void whatever(vector&) = 0;};struct B : A {  void whatever(list& x) { impl(x.begin(), x.end()); }  void whatever(vector& x) { impl(x.begin(), x.end()); }  private:  template <typename It>  void impl(It begin, It end) {    for (; begin != end; ++begin) { doSomethingTo(*begin);    }  }};;  If you just need to manipulate the elements of the container, pass an iterator range.