U E D R , A S I H C RSS

벡터/권정욱

벡터/권정욱


데블스캠프/목요일/스튜던트 정렬하기

~cpp 
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;

struct student{
	string name;
	int score;
};
bool compare(student a, student b)
{
	return a.score>b.score;
}

int main(){
	student student_num1;
	student student_num2;
	student student_num3;
	student student_num4;
	student student_num5;
	student_num1.name = "Park Jin-young";
	student_num1.score = 80;
	student_num2.name = "Kwon Jung-wook";
	student_num2.score = 100;
	student_num3.name = "Lee Jae-hwan";
	student_num3.score = 80;
	student_num4.name = "Kim Su-jin";
	student_num4.score = 70;
	student_num5.name = "Kim Hong-bem";
	student_num5.score = 90;

	vector<student> vec;
	vec.push_back(student_num1);
	vec.push_back(student_num2);
	vec.push_back(student_num3);
	vec.push_back(student_num4);
	vec.push_back(student_num5);

	sort(vec.begin(), vec.end(), compare);

	vector<student>::iterator i=vec.begin(); 	
	for(i = vec.begin(); i != vec.end(); i++) 
	{ 
		cout << (*i).name << "의 성적은 : " << (*i).score << endl;
	} 

	return 0;
}

STL실습
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:35
Processing time 0.0162 sec