#include <iostream>
#include <string>
template<typename T>
void swapValue(T & a, T & b) {
T temp = a;
a = b;
b = temp;
}
void swapValue(float & a, float & b) {
float temp = a;
a = b;
b = temp;
}
void swapValue(char & a, char & b) {
char temp = a;
a = b;
b = temp;
}
void swapValue(string & a, string & b) {
string temp = a;
a = b;
b = temp;
}
int main() {
int a = 10, b = 20;
swapValue(a, b);
std::cout << "交换后:a=" << a << ", b=" << b << std::endl;
float c = 3.14, d = 6.28;
swapValue(c, d);
std::cout << "交换后:c=" << c << ", d=" << d << std::endl; // 输出:c=6.28, d=3.14
char e = 'A', f = 'B';
swapValue(e, f);
std::cout << "交换后:e=" << e << ", f=" << f << std::endl; // 输出:c=6.28, d=3.14
return 0;
}
—— 本文来自火龙信奥(义乌睿码科技):义乌青少年信息学奥赛与编程教育平台,专注 CSP-J/S、NOIP、GESP 竞赛培训,线上线下融合教学,助力编程升学。网址:hlcoding.com