Function that takes an STL iterator over ANY container of a elements of a specific type

前端 未结 5 2092
刺人心
刺人心 2021-02-12 19:12

How would I define a function that takes as input an iterator over any type of STL container, but only to those of a specific templated type. For example:

Any iterator o

5条回答
  •  轮回少年
    2021-02-12 19:27

    Well i would use a simple typedef in the case that you only need one container-type at the time (if i understood your usecase correctly this is the case)

    template 
    class ContainerIterator
    {
      Container();
      public:
        typedef std::list::iterator type;
    }
    
    //usage:
    void function(ContainerIterator::type begin, ContainerIterator::type end)
    {
      //...
    }
    

    to switch the container later on, simply change the container type in the typedef.

提交回复
热议问题