Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

벡터/황재선: Difference between revisions

From ZeroWiki
imported>Unknown
No edit summary
 
(Repair batch-0005 pages from live compare)
 
Line 17: Line 17:
  {
  {
   
   
  student stu[5];
  student stu[5];
  stu[0].name = "황재선";
  stu[0].name = "황재선";
  stu[0].score = 1;
  stu[0].score = 1;
   
   
  stu[1].name = "조재화";
  stu[1].name = "조재화";
  stu[1].score = 10;
  stu[1].score = 10;
 
 
  stu[2].name = "곽세환";
  stu[2].name = "곽세환";
  stu[2].score = 6;
  stu[2].score = 6;
 
 
  stu[3].name = "김회영";
  stu[3].name = "김회영";
  stu[3].score = 4;
  stu[3].score = 4;
   
   
  stu[4].name = "김회광";
  stu[4].name = "김회광";
  stu[4].score = 5;
  stu[4].score = 5;
 
 
  vector<student> ss;
  vector<student> ss;
  ss.push_back(stu[0]);
  ss.push_back(stu[0]);
  ss.push_back(stu[1]);
  ss.push_back(stu[1]);
  ss.push_back(stu[2]);
  ss.push_back(stu[2]);
  ss.push_back(stu[3]);
  ss.push_back(stu[3]);
  ss.push_back(stu[4]);
  ss.push_back(stu[4]);
   
   
  sort(ss.begin(), ss.end(), compareWithName);
  sort(ss.begin(), ss.end(), compareWithName);
Line 67: Line 67:


----
----
[[STL실습]], [[데블스캠프2004/목요일]]
STL실습, 데블스캠프2004/목요일
 

Latest revision as of 00:44, 27 March 2026

#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/목요일