C++ exchange
时间: 2023-07-28 13:09:32 浏览: 111
一个用c++写的交换排序
Sure, I can help you with that! In C++, you can exchange the values of two variables using a temporary variable. Here's an example:
```cpp
#include <iostream>
void exchange(int& a, int& b) {
int temp = a;
a = b;
b = temp;
}
int main() {
int num1 = 5;
int num2 = 10;
std::cout << "Before exchange: num1 = " << num1 << ", num2 = " << num2 << std::endl;
exchange(num1, num2);
std::cout << "After exchange: num1 = " << num1 << ", num2 = " << num2 << std::endl;
return 0;
}
```
In this example, the `exchange` function takes two integers by reference and swaps their values using a temporary variable. You can call this function to exchange the values of any two variables.
阅读全文