Declare a function accepting generic iterator

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

    Please try this, this would work in all container:

    template
    void disp(T &t)
    {
        for( auto itr=t.begin();itr!=t.end();itr++)
            cout<<*itr< v(3,77);
        list l(5,"Hello");
        disp(l)
        disp(v);
    

    Note: Don't forget to include<string>,
    And auto is available in c++ 11

提交回复
热议问题