C++学习 函数模板及重载

来源:互联网 发布:快读免费小说网络错误 编辑:程序博客网 时间:2024/06/05 20:06
第一篇博客,好感动!
这段代码希望能够自动为各种类型重载输出函数。
#include <iostream>#include <set>#include <map>#include <queue>#include <string>#include <vector>const int MAXN = 10;using namespace std;struct job{char name[50];int salary;};template <typename T>void show(const T& a){cout << a << endl;};template <typename T>void show(const T(&a)[MAXN]){for (auto elem : a){cout << elem << ' ';}cout << endl;};template <typename T>void show(T a, int n){for (int i = 0; i < n; i++){cout << a[i] << ' ';}cout << endl;};template <typename T>void show(vector<T>& a){for (auto elem : a){cout << elem << ' ';}cout << endl;}void show(vector<job>& a){for (auto i : a){cout << i.name << '\t'<< i.salary << endl;}}int main(){int arr[MAXN] = { 1, 2, 3 };vector<int> vec1 = { 1, 2, 3 };vector<job> vec2 = {{ "a", 1 },{ "b", 2 },{ "c", 3 }};show(3);show("abc");show(arr);show(arr, 3);show(vec1);show(vec2);system("pause");}


0 0
原创粉丝点击