use template to print all the data of any container

后端 未结 2 1084
隐瞒了意图╮
隐瞒了意图╮ 2021-01-28 05:02

I plan to write a function which can print all the data of any container. In other words, I can use with different containers type like vector, deque or list and that I can call

2条回答
  •  孤城傲影
    2021-01-28 05:19

    Using c++11 you can simplify it to:

    template 
    void print(const C &data){
        for (auto &elem : data)
            cout << elem << " ";
        cout << endl;
    }
    

    and call it with print(data). You could also use std::for_each or copy it directly into cout.

提交回复
热议问题