Declare a function accepting generic iterator

后端 未结 4 699
慢半拍i
慢半拍i 2021-01-31 10:17

Given this code, is it possible to change dumpStrings() to be able to iterate over any container of string, like say a list

4条回答
  •  星月不相逢
    2021-01-31 10:37

    I don't think there's anything as simple as you'd like. Ideally, you could just do something like

    void dumpStrings(AbstractIterator beg, AbstractIterator end) { }
    

    but the STL iterators don't seem to have any inheritance hierarchy, irritatingly enough. So it looks like you're stuck with using function templates - that's how it's done in the STL Algorithms Library.

    Sorry - I wish there was a better way, too, but this'll have to do. Just remember to declare the full function template in the header file!

提交回复
热议问题