字符串排序(1)
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cin >> n;
vector<string> vc;
while (n--) {
string temp;
cin >> temp;
vc.push_back(temp);
}
sort(vc.begin(), vc.end());
for (int i = 0; i < vc.size(); i++) {
if (i != vc.size() - 1) {
cout << vc[i] << " ";
} else {
cout << vc[i];
}
}
return 0;
}
字符串排序(2)
#include <bits/stdc++.h>
using namespace std;
int main() {
string str;
while (getline(cin, str)) {
stringstream ss(str);
vector<string> vc;
string temp;
while (getline(ss, temp, ' ')) {
vc.push_back(temp);
}
sort(vc.begin(), vc.end());
for (auto n : vc) {
cout << n << " ";
}
cout << endl;
}
return 0;
}
字符串排序(3)
#include <bits/stdc++.h>
using namespace std;
int main() {
string line;
while (getline(cin, line)) {
stringstream ss(line);
vector<string> vc;
string temp;
while (getline(ss, temp, ',')) {
vc.push_back(temp);
}
sort(vc.begin(), vc.end());
for (int i = 0; i < vc.size(); i++) {
if (i != vc.size() - 1) {
cout << vc[i] << ",";
} else {
cout << vc[i] << endl;
}
}
}
return 0;
}