C++ std::swap函数的使用

功能描述

交换两个变量的值,譬如参数A和参数B,把A的值赋给B,再把B的值赋给A参数

函数原型  

 swap(_Tp& __a, _Tp& __b)

参数释义

  • - 参数  __a  一个任意类型的参数.
  • - 参数  __b  另一个任意类型的参数.
  • - 返回值   无

源码示例

   #include <iostream>
    #include <algorithm>
    
    int main(){
        int x = 10;
        int y = 20;
        std::cout<<"before swap: x:"<<x<<" y:"<<y<<std::endl;
        std::swap(x, y);
        std::cout<<"after swap: x:"<<x<<" y:"<<y<<std::endl;
    }

输出结果


 

猜你喜欢

转载自blog.csdn.net/jndingxin/article/details/110236364