zl程序教程

您现在的位置是:首页 >  后端

当前栏目

C++之swap()使用(一百一十四)

C++ swap 一百一十 使用
2023-09-14 09:09:57 时间

1.代码示例 

#include <iostream>
using namespace std;

int main () {
	//1.swap int val
  int x=10, y=20; // x:10 y:20
  swap(x,y); //x:20 y:10
	cout << "x = " << x << "x = " << y << endl;

	//2.swap str val
  int foo[4] = {1,2,3,4};
  int bar[] = {10,20,30,40};
  swap(foo,bar); //foo: 10,20,30,40; bar:1,2,3,4,

	cout << "foo: ";
  for(int i: foo)
		cout << ' ' << i;
	cout << endl;

	cout << "bar: ";
	for(auto i: bar)
		cout << ' ' << i;

  return 0;
}