본문으로 바로가기

C++ STL sort( ) 함수 다루기

category onYouTube/Algorithm 2021. 4. 2. 20:24

< sort( ) 함수의 기본 사용법 >

  • sort( ) 함수는 C++ algorithm 헤더에 포함되어 있음
  • sort( ) 함수는 기본적으로 오름차순 정렬을 수행
  • sort(배열의 시작점 주소, 마지막 주소 + 1)
#include <iostream>
#include <algorithm>


using namespace std;


int main(void){

	int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2};


	sort(a, a+10);


	for(int i = 0; i < 10; i++) {

		cout << a[i] << ' ';
	}
}

 

  • compare( ) 함수를 만들어 sort( )의 세번째 인자 값으로 넣으면, 해당 함수의 반환 값에 맞게 정렬이 동작
#include <iostream>
#include <algorithm>


using namespace std;


bool compare(int a, int b) {

	return a < b; // 오름차순
}



int main(void) {

	int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2};
    
    
	sort(a, a+10, compare);


	for(int i = 0; i < 10; i++) {
    
		cout << a[i] << ' ';
	}
}
#include <iostream>
#include <algorithm>


using namespace std;


bool compare(int a, int b) {

	return a > b; // 내림차순
}



int main(void) {

	int a[10] = {9, 3, 5, 4, 1, 10, 8, 6, 7, 2};
    
    
	sort(a, a+10, compare);


	for(int i = 0; i < 10; i++) {
    
		cout << a[i] << ' ';
	}
}

< 데이터를 묶어서 특정한 변수를 기준으로 정렬하는 방법 >

#include <iostream>
#include <algorithm>


using namespace std;


class Student{
public:
	string name;
	int score;
    
    
	Student(string name, int score) {
		this->name = name;
		this->score = score;
	}


	bool operator <(Student &student) { // 정렬 기준은 '점수가 낮은 순서'
		return this->score<student.score;
	}
};



int main(void) {

	Student students[] = {
    
		Student("김제니", 90),
		Student("김지수", 93),
		Student("박채영", 97),
		Student("라리사", 87)
	};


	sort(students, students + 4);
    
    
	for(int i = 0; i < 5; i++) {
    
		cout << students[i].name << ' ';
	}
}

 

  • 클래스를 이용하는 방식은 실무에 적합한 방식이며,
    프로그래밍 대회와 같은 빠른 개발이 필요할 때는 페어(Pair) 라이브러리를 사용하는 것이 더 효율적

 

  • "벡터(Vector) STL" : 배열을 보다 사용하기 쉽게 개편한 자료구조
  • "페어(Pair) STL" : 한 쌍의 데이터를 처리할 수 있도록 해주는 자료구조
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;


int main(void) {

	vector<pair<int, string>> v;


	v.push_back(pair<int, string> (90, "김제니"));
	v.push_back(pair<int, string> (92, "김지수"));
	v.push_back(pair<int, string> (97, "박채영"));
	v.push_back(pair<int, string> (87, "라리사"));


	sort(v.begin(), v.end());
    
    
	for(int i = 0; i < v.size(); i++) {
    
		cout << v[i].second << ' ';
	}


	return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>


using namespace std;


bool compare(pair<string, pair<int, int>> a, piar<string, pair<int, int>> b) {

	if(a.second.first == b.second.first) {
		return a.second.second > b.second.second;
	}
	else {
		return a.second.first > b.second.first;
	}
}



int main(void) {

	vector<pair<string, pair<int, int>>> v;


	v.push_back(pair<string, pair<int, string>> ("김제니", make_pair(90, 19960116));
	v.push_back(pair<string, pair<int, string>> ("김지수", make_pair(92, 19950103));
	v.push_back(pair<string, pair<int, string>> ("박채영", make_pair(97, 19970211));
	v.push_back(pair<string, pair<int, string>> ("라리사", make_pair(87, 19970327));


	sort(v.begin(), v.end(), compare);
    
    
	for(int i = 0; i < v.size(); i++) {
    
		cout << v[i].first << ' ';
	}


	return 0;
}

'onYouTube > Algorithm' 카테고리의 다른 글

계수 정렬(Counting Sort)  (0) 2021.04.04
힙 정렬(Heap Sort)  (0) 2021.04.03
병합 정렬(Merge Sort)  (0) 2021.04.02
기초 정렬 알고리즘 문제 풀이  (0) 2021.04.02
퀵 정렬(Quick Sort)  (0) 2021.04.02