stl map的用法(在STL中,map按值来排序的实现方法)
在STL中 ,map是按键来排序的 ,但很多时候需要按值来排序 。一种方法是将map转化为vector ,然后排序 。
tool.h
#ifndef TOOL_H
#define TOOL_H
#include
#include
#include
#include
#include
using namespace std;
void sortMapByValue(map
tool.cpp
#include "tool.h"
int cmp(const pair
{
return x.second
}
void sortMapByValue(map
{
for(map
{
tVector.push_back(make_pair(curr->first,curr->second));
}
sort(tVector.begin(),tVector.end(),cmp);
}本文来自CSDN博客 ,转载出处:http://blog.csdn.net/andyelvis/archive/2009/07/19/4361962.aspx
创心域SEO版权声明:以上内容作者已申请原创保护,未经允许不得转载,侵权必究!授权事宜、对本内容有异议或投诉,敬请联系网站管理员,我们将尽快回复您,谢谢合作!