电脑网络

c++怎么写这个代码?

346 浏览

c++怎么写这个代码?

1 个回答

liеenow用户头像
liеenow 回答于 2024-07-20
已采纳

提问:c++怎么写这个代码?

网友回答:

swap是C++标准函数,不能重定义,改个名(出题教程太low了)

template <class T>

void mswap(T& x, T& y)

{

    T tmp = x;

    x = y;

    y = tmp;

}

这个是C++模板最最基础的应用了

测试程序

int main()

{

    int a=1,b=2;

    mswap(a,b);

    cout << a << "," << b << endl;

    double x=1.2,y=3.4;

    mswap(x,y);

    cout << x << "," <<y << endl;

    char c='a',d='b';

    mswap(c,d);

    cout << c << "," <<d << endl;

    return 0;

}

测试结果

我来回答

相关问题