c++, 조건부 sort하기
글쓴이: minyoung347 / 작성시간: 목, 2016/09/29 - 9:51오후
안녕하세요,
대용량 문서를 sort하려고 하는데
조건을 줘서 여러번 분류해야 하는 상황이 생겼습니다.
설명을 드리기 위해서 코드를 간단하게 만들었습니다.
test.csv 파일에는 6명의 성적이 있습니다.
각각의 학생은 class_A 또는 class_B에서 속해 있습니다.
제가 하고 싶은 것은 6명의 학생을 class별로 먼저 분류하고
그 다음에 성적별로 분류하는 것입니다.
아래의 코드와 같이 하면 높은 성적을 받은 학생이 위로 올라오게 됩니다.
그런데 문제는 class_A는 높은 점수을 받은 학생이 잘하는 것이고
class_B는 낮은 점수를 받은 학생이 잘하는 것입니다.
잘하는 학생을 위로 오게 하려면 어떻게 하는지 궁금합니다.
요약하자면, class별로 다른 기준으로 sort하는 방법을 알고싶습니다.
(class_A는 내림차순, class_B는 오름차순)
(code의 Compare_By_string함수에서 string1을 기준으로 class를 분류하고 string2를 기준으로 성적을 분류합니다)
방법을 알려주시면 감사하겠습니다.
(필요없는 헤더파일이 있을 수 있습니다..)
-------test.csv---------
class_A,90
class_B,10
class_A,70
class_B,30
class_A,80
class_B,20
------------------------
#include <iostream> #include <fstream> #include <sstream> #include <string> #include <sys/stat.h> #include <cstdio> #include <cstdlib> #include <cmath> #include <iomanip> #include <vector> #include <algorithm> #include <iterator> #include <unistd.h> using namespace std; class line_class { public: line_class(const string& init_string1, const string& init_string2); friend int Compare_By_string(const line_class& lhs, const line_class& rhs); friend ostream& operator<<(ostream& os, const line_class& obj) { os<<obj.string1<<','<<obj.string2; return os; } private: string string1, string2; }; line_class::line_class(const string& init_string1, const string& init_string2) :string1(init_string1), string2(init_string2){} int Compare_By_string(const line_class& lhs, const line_class& rhs) { if(lhs.string1==rhs.string1) { return (lhs.string2>rhs.string2); } else if(lhs.string1!=rhs.string1) { return (lhs.string1<rhs.string1); } } int main() { ostringstream stream; string cc; const char* ccc; stream.str(""); stream<<"./test.csv"; cc=stream.str(); ccc=cc.c_str(); ifstream file(ccc); vector<line_class> vector_line; string string1, string2; while(getline(file,string1,',') &&getline(file,string2,'\n')) { vector_line.push_back(line_class(string1, string2)); } file.close(); sort(vector_line.begin(), vector_line.end(),Compare_By_string); stream.str(""); stream<<"./result.csv"; cc=stream.str(); ccc=cc.c_str(); ofstream fout(ccc,ios::app); std::ostream_iterator<line_class> output_iterator(fout,"\n"); std::copy(vector_line.begin(),vector_line.end(),output_iterator); fout.close(); return 0; }
Forums:
뭐, 예컨대
클래스 종류가 얼마 안된다면 이렇게 하드코딩해버릴 수도 있죠.
얼마나 유연하게 만들어야 하느냐에 따라 달라진다고 볼 수 있겠습니다.
친절한 설명 감사합니다
덕분에 코드를 간결하게 만들었습니다. 너무 감사합니다!
댓글 달기