구조체를 만들고 이를 vector형태로 만들고 string과 int 값을 부여한다. int값을 기준으로 내림차순으로 sotring하고 출력한다.
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;
typedef struct sorting
{
string id;
int count;
} t_sorting;
bool compare(t_sorting i,t_sorting j)
{
return (i.count > j.count);
}
int main () {
vector<sorting> str;
sorting k;
k.id = "hello";
k.count = 3;
str.push_back(k);
k.id = "world";
k.count = 1;
str.push_back(k);
k.id = "c++";
k.count = 2;
str.push_back(k);
stable_sort(str.begin(), str.end(),compare);
for(vector<t_sorting>::iterator it = str.begin(); it!= str.end(); ++it)
{
cout << it->id << " " << it->count << endl;
}
return 0;
}
count가 높은 순서대로 sorting되어 해당하는 string과 count를 보여준다.
댓글