下面的程序演示了如何使用这个库模板函数来交换两个变量的内容:
// This program demonstrates the use of the swap function template. #include <iostream> #include <string> #include <algorithm> // Needed for swap u sin g namespace std; int main () // Get and swap two chars char firstChar, secondChar; cout << "Enter two characters: "; cin >> firstChar >> secondChar; swap(firstChar, secondChar); cout << firstChar << " " << secondChar << endl; // Get and swap two ints int firstInt, secondInt; cout << "Enter two integers: "; cin >> firstInt >> secondInt; swap(firstInt, secondInt); cout << firstInt << " " << secondInt << endl; // Get and swap two strings cout << "Enter two strings: "; string firstString, secondString; cin >> firstString >> secondString; swap(firstString, secondString); cout << firstString << " " << secondString << endl; return 0; 程序输出结果: Enter two characters: a b
Enter two integers: 12 45
45 12
Enter two strings: http://c.biancheng.net cyuyan
cyuyan http://c.biancheng.net

关注公众号「 站长严长生 」,在手机上阅读所有教程,随时随地都能学习。本公众号由 C语言中文网站长 亲自运营,长期更新,坚持原创。

微信扫码关注公众号
  • Go语言从切片中删除元素
  • C语言和C++到底有什么关系?
  • MySQL WEEK函数:获取指定日期是一年中的第几周
  • Python readline()和readlines()函数:按行读取文件
  • Python Process创建进程(2种方法)详解
  • python不是内部或外部命令的解决方法
  • Python seek()和tell()函数详解
  • Linux Vim可视化模式及其用法
  • GCC是什么?
  • Pandas Series入门教程
  •