修改以下代码,使得代码变得简洁并完成题目要求#include<iostream> #include<algorithm> #include<vector> using namespace std; template<typename T> void f1(vector<T>&x) { sort(x.begin(), x.end()); for (int i = 0; i < x.size(); i++) { cout << x[i] << " "; } } template<typename T> void f0(vector<T>& x) { sort(x.begin(), x.end(), greater<T>()); for (int i = 0; i < x.size(); i++) { cout << x[i] << " "; } } int main() { int a, b; while (cin >> a >> b) { if (b == 0) { int x; vector<int>arr; while (cin>>x) { arr.push_back(x); if (cin.get() == '\n') { break; } } for (int i = 0; i < arr.size(); i++) { cout << arr[i] << " "; } cout << endl; if (a) { f1(arr); } else { f0(arr); } vector<int>().swap(arr); cout << endl; } else if (b == 1) { double x; vector<double>arr1; while (cin >> x) { arr1.push_back(x); if (cin.get() == '\n') { break; } } for (int i = 0; i < arr1.size(); i++) { cout << arr1[i] << " "; } cout << endl; if (a) { f1(arr1); } else { f0(arr1); } vector<double>().swap(arr1); cout << endl; } else{ char x; vector<char>arr2; while (cin >> x) { arr2.push_back(x); if (cin.get() == '\n') { break; } } for (int i = 0; i < arr2.size(); i++) { cout << arr2[i] << " "; } cout << endl; if (a) { f1(arr2); } else { f0(arr2); } vector<char>().swap(arr2); cout << endl; } } return 0; }
时间: 2024-03-03 07:47:09 浏览: 69
以下是修改后的代码:
```cpp
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
template<typename T>
void f(vector<T>& x, bool reverse) {
sort(x.begin(), x.end(), (reverse ? greater<T>() : less<T>()));
for (const auto& i : x) {
cout << i << " ";
}
cout << endl;
}
int main() {
int a, b;
while (cin >> a >> b) {
if (b == 0) {
int x;
vector<int>arr;
while (cin >> x) {
arr.push_back(x);
if (cin.get() == '\n') {
break;
}
}
for (const auto& i : arr) {
cout << i << " ";
}
cout << endl;
f(arr, a);
arr.clear();
}
else if (b == 1) {
double x;
vector<double>arr1;
while (cin >> x) {
arr1.push_back(x);
if (cin.get() == '\n') {
break;
}
}
for (const auto& i : arr1) {
cout << i << " ";
}
cout << endl;
f(arr1, a);
arr1.clear();
}
else {
char x;
vector<char>arr2;
while (cin >> x) {
arr2.push_back(x);
if (cin.get() == '\n') {
break;
}
}
for (const auto& i : arr2) {
cout << i << " ";
}
cout << endl;
f(arr2, a);
arr2.clear();
}
cout << endl;
}
return 0;
}
```
修改后的代码将函数 `f1` 和 `f0` 合并成了一个函数 `f`,并使用了模板参数 `reverse`,代替原先的 `a` 参数,以指示是否需要逆序排序。同时,使用了 `const auto&` 循环遍历容器内元素,使代码更加简洁。在清空容器时,使用了 `clear()` 函数。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)