Declare a function accepting generic iterator

后端 未结 4 690
慢半拍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:38

    Create a template

    template
    void dumpStrings(iterator_type it, iterator_type end)
    {
        while (it != end) {
            cout << *(it++) << endl;
        }
    }
    

    The template also removes the limit of the container value type to string. Note that you need the parentheses around the it++.

提交回复
热议问题