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 compareWithName(student a, student b);
bool compareWithScore(student a, student b);

int main()
{

	student stu[5];
	stu[0].name = "황재선";
	stu[0].score = 1;

	stu[1].name = "조재화";
	stu[1].score = 10;
	
	stu[2].name = "곽세환";
	stu[2].score = 6;
	
	stu[3].name = "김회영";
	stu[3].score = 4;

	stu[4].name = "김회광";
	stu[4].score = 5;
	
	vector<student> ss;
	ss.push_back(stu[0]);
	ss.push_back(stu[1]);
	ss.push_back(stu[2]);
	ss.push_back(stu[3]);
	ss.push_back(stu[4]);

	sort(ss.begin(), ss.end(), compareWithName);

	for (vector<student>::iterator i = ss.begin(); i < ss.end(); i++)	// 오름차순
		cout << (*i).name << "\t" << (*i).score << endl;

	cout << endl;
	sort(ss.begin(), ss.end(), compareWithScore);
	
	for (i = ss.begin(); i < ss.end(); i++)	// 오름차순
		cout << (*i).name << "\t" << (*i).score << endl;

	return 0;
}

bool compareWithName(student a, student b)
{
	return a.name < b.name;

}

bool compareWithScore(student a, student b)
{
	return a.score < b.score;
	
}


STL실습, 블스캠프2004/목요일
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:29:35
Processing time 0.0124 sec