[ create a new paste ] login | about

Link: http://codepad.org/fIJmmwrv    [ raw code | fork ]

C++, pasted on Apr 17:
#include <fstream>
#include <string>
#include <sstream>
#include <algorithm>
#include <vector>
using namespace std;
struct Data { string c1,c2,c3; };
bool line_sort(const Data* l, const Data* r) {  return l->c1 < r->c1; }
int main(int argc, char const* argv[])
{
  vector<Data*> v;
  ifstream ifs("in.txt");
  string line;
  stringstream ss;
  if(ifs) {
    while( getline(ifs,line) ) {
      replace(line.begin(),line.end(),' ','@');
      replace(line.begin(),line.end(),'<',' ');
      ss.str(line);
      Data *d = new Data();
      ss >> d->c1 >> d->c2 >> d->c3;
      if(ss.fail())
        break;
      v.push_back(d); 
      ss.str("");
      ss.clear(stringstream::goodbit);
    }
		ifs.close();
  }
//  stable_sort(v.begin(), v.end(), line_sort);
  sort(v.begin(), v.end(), line_sort);
  ofstream ofs("out.txt");
	string out;
	if(ofs) {
		vector<Data*>::iterator itr;
		for(itr=v.begin(); itr!=v.end(); itr++){ 
			replace( (*itr)->c1.begin(), (*itr)->c1.end(), '@', ' ' );
			out += (*itr)->c1 + "<" + (*itr)->c2 + "<" + (*itr)->c3 + "\n";
		}
		ofs << out;
		ofs.close();
	}
  return 0;
}


Output:
No errors or program output.


Create a new paste based on this one


Comments: