메뉴 닫기

CMAP Sort

#include <iostream>
#include <map>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class sort_map
{
  public:
	string key;
	int val;
};
bool Sort_by(const sort_map& a ,const sort_map& b)
{
	return a.val < b.val;
}
int main()
{
	map<string,int> d;
	map<string,int>::iterator it;
	vector< sort_map > v;
	vector< sort_map >::iterator itv;
	sort_map sm;
	d["a"] = 5; d["b"] = 3; d["c"] = 9; d["d"] = 7; d["e"] = 1; d["f"] = 13; d["g"] = 11;
	for (it = d.begin(); it != d.end(); ++it)
	{
		sm.key = (*it).first; sm.val = (*it).second;
		v.push_back(sm);
	}
	for (itv = v.begin(); itv != v.end(); ++itv)
	{
		cout << (*itv).key << " : " << (*itv).val << endl;
	}
	sort(v.begin(),v.end(),Sort_by);
	cout << "sorted" << endl;
	for (itv = v.begin(); itv != v.end(); ++itv)
	{
		cout << (*itv).key << " : " << (*itv).val << endl;
	}
	return 0;
}

5 Comments

  1. Herschel Zvorsky

    I simply want to say I am beginner to blogs and definitely loved your website. Almost certainly I’m likely to bookmark your blog . You absolutely have beneficial well written articles. Thank you for sharing your website.

  2. website hosting

    Undeniably believe that which you stated. Your favorite reason appeared to be on the web the simplest thing
    to be aware of. I say to you, I definitely
    get annoyed while people consider worries that
    they just do not know about. You managed to hit the nail
    upon the top as well as defined out the whole thing without having side effect
    , people could take a signal. Will probably be back to get more.
    Thanks

댓글 남기기

이메일은 공개되지 않습니다.