<-------------------- Пометил вопросы.
Код | #include "stdafx.h" #include <fstream>
#include <vector> #include <string> #include <algorithm>
using namespace std;
bool Mycompare ( string elem1, string elem2 ) { return elem1 > elem2; }
int _tmain(int argc, _TCHAR* argv[]) { /* ifstream c("input.txt"); ofstream d("output2.txt"); char e[255]; char **aa, **bb; <------------------------------ что обозначают ** int j, m;
// Первоначальное отведение памяти m=10; aa = new char*[m];
int i=0; if (c.is_open()<------------------------------ что выполняет) { for (; !c.eof();) { c.getline(e, 254); <------------------------------ что выполняет aa[i]=new char[strlen(e)+1]; strcpy(aa[i], e); i++; if (i>=m) { // Отведение дополнительной памяти bb = new char*[m+10]; for (j=0; j<m; j++) bb[j]=aa[j]; m+=10; delete [] aa; aa = new char*[m]; for (j=0; j<m-10; j++) aa[j]=bb[j]; delete [] bb; <---------------------------------- обязательно ли тут скобки [], почему? } }
// сортировка int n; char *w; for (int k=0; k<i-1; k++) { n=k; for (int l=k+1; l<i; l++) if (strcmp(aa[n], aa[l])<0) n=l; if (n>k) { w=aa[k]; aa[k]=aa[n]; aa[n]=w; } } // вывод for (j=0; j<i; j++) d<<aa[j]<<endl;
// освобождение памяти for (j=0; j<i; j++) delete [] aa[j]; delete [] aa; } c.close(); d.close(); */
// stl
ifstream c("input.txt"); ofstream d("output1.txt"); vector<string> aa; char ee[255];
//input if (c.is_open()) { while(!c.eof()) { c.getline(ee, 254); aa.push_back(ee); <------------------------------ что тут происходит? } // sort sort(aa.begin(), aa.end(), Mycompare);
//output // for (unsigned int j=0; j<aa.size(); j++) dd<<aa[j].c_str()<<endl; for (vector<string>::iterator j=aa.begin(); j!=aa.end(); j++) d<<(*j).c_str()<<endl; } c.close(); d.close();
return 0; }
|
|