c++的swap函数怎么使用

   2024-10-20 4010
核心提示:在C++中,可以使用std::swap函数来交换两个变量的值。以下是使用std::swap函数的示例代码:#include iostream#include algorithm

在C++中,可以使用std::swap函数来交换两个变量的值。以下是使用std::swap函数的示例代码:

#include <iostream>#include <algorithm>int main() {    int a = 5;    int b = 10;    std::cout << "Before swapping: a = " << a << ", b = " << b << std::endl;    std::swap(a, b);    std::cout << "After swapping: a = " << a << ", b = " << b << std::endl;    return 0;}

运行上面的代码将输出:

Before swapping: a = 5, b = 10After swapping: a = 10, b = 5

可以看到,使用std::swap函数可以轻松交换两个变量的值。

 
举报打赏
 
更多>同类网点查询
推荐图文
推荐网点查询
点击排行

网站首页  |  关于我们  |  联系方式网站留言    |  赣ICP备2021007278号