《编程珠玑》第二章 问题C
#include<iostream>#include<fstream>#include<algorithm>#include<vector>using namespace std;static bool com(pair<string, string> a, pair<string, string> b) {if (a.fir
·
#include<iostream>
#include<fstream>
#include<algorithm>
#include<vector>
using namespace std;
static bool com(pair<string, string> a, pair<string, string> b) {
if (a.first != b.first) return a.first < b.first;
else return a.second < b.second;
}
static bool com1(string a, string b) {
return a < b;
}
int main() {
ifstream in("text1.txt");
string a;
vector<string> sig;
while (in >> a) sig.push_back(a);
ofstream out("text2.txt");
vector<pair<string, string>> sor;
for (int i = 0; i < sig.size(); i++) {
string temp = sig[i];
sort(temp.begin(), temp.end());
pair<string, string> pa(temp, sig[i]);
sor.emplace_back(pa);
}
sort(sor.begin(), sor.end(), com);
for (int i = 0; i < sor.size(); i++)
out << sor[i].first << " " << sor[i].second << endl;
ofstream out1("text3.txt");
string temp = sor[0].first;
int linenum = 0;
for (int i = 0; i < sor.size(); i++) {
if (sor[i].first != temp && linenum > 0) {
out1 << endl;
temp = sor[i].first;
}
out1 << sor[i].second << " ";
//temp = sor[i].second;
linenum++;
}
return 0;
}
更多推荐



所有评论(0)