知识库

C++语言开发示例:17. 临时函数

2025-02-01 20:35:18 李腾 1 次阅读

funtemp.cpp

#include <iostream>
template <typename T>
void Swap(T &a, T &b);

int main()
{
    using namespace std;
    int i = 10;
    int j = 20;
    cout << "i, j = " << i << ", " << j << ".\n";
    cout << "Using compiler-generated int swapper:\n";
    Swap(i, j);
    cout << "Now i, j = " << i << ", " << j << ".\n";

    double x = 24.5;
    double y = 81.7;
    cout << "x, y = " << x << ", " << y << ".\n";
    Swap(x, y);
    cout << "Now x, y = " << x << ", " << y << ".\n";
}

template <typename T>
void Swap(T &a, T &b)
{
    T temp;
    temp = a;
    a = b;
    b = temp;
}
转载请注明转自www.hylab.cn,原文地址:C++语言开发示例:17. 临时函数

评论 (0)

登录后发表评论

暂无评论,快来发表第一条评论吧!