发布时间:2022-07-05 文章分类:编程知识 投稿人:赵颖 字号: 默认 | | 超大 打印
map中按值排序---泛型排序
2011-12-11 19:16

template <typename T>

inline int cmp(pair<T,int>& v1,pair<T,int>& v2)

{

return v1.second > v2.second;

}

template <typename T>

inline void SortMapByValue(map<T,int>& mapImgKeyWordsVSM,

vector<pair<T,int>>& imgVSMVec)

{

for (map<T,int>::iterator curr = mapImgKeyWordsVSM.begin(); curr != mapImgKeyWordsVSM.end(); curr++)

{

imgVSMVec.push_back(std::make_pair(curr->first, curr->second));

}

std::sort(imgVSMVec.begin(), imgVSMVec.end(), cmp<T>);

}

//分词

#include <boost\regex.hpp>

#include <boost\algorithm\string.hpp>

void GetVSMVectorMap(const string&strVSMTxt ,map<string,int>&keyFreMap)

{

keyFreMap.clear();

boost::regex expression(",");

boost::sregex_token_iterator it(strVSMTxt.begin(),strVSMTxt.end(),expression,-1);

boost::sregex_token_iterator itEnd;

while (it!=itEnd)

{

string strKeyFreTxt = *it;

boost::trim(strKeyFreTxt);

if (!strKeyFreTxt.empty())

{

int nKeyWordEPos = strKeyFreTxt.find_first_of(':');

assert(nKeyWordEPos>0);

string strKeyWord = strKeyFreTxt.substr(0,nKeyWordEPos);

string strFre = strKeyFreTxt.substr(nKeyWordEPos+1);

int iFre = atol(strFre.c_str());

keyFreMap.insert(make_pair(strKeyWord,iFre));

}

it++;

}

return;

}

map<pair<string,string>,int>::iterator itSysVSM = mapImgSysVSM.find(it->first);