Discuz! Board

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1775|回复: 0
打印 上一主题 下一主题

统计出现次数和对map按value排序

[复制链接]

257

主题

354

帖子

1677

积分

金牌会员

Rank: 6Rank: 6

积分
1677
跳转到指定楼层
楼主
发表于 2016-5-2 12:09:59 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. CDR_PILOT stPilotTemp;
  2.         CDR_CELL_EX stCellExTemp;
  3.         //map<__int64, CDR_CELL_EX> m_mapCellEx;
  4.        
  5.         for( map<string, CDR_CELL>::iterator itor = m_mapCell.begin(); itor != m_mapCell.end(); itor++ )
  6.         {
  7.                         vector<CDR_CELL_EX> vec_CellEx;
  8.                         for( size_t i = 0; i < itor->second.vec_pilot_other.size() ; i++ )
  9.                         {
  10.                                 stPilotTemp = itor->second.vec_pilot_other[i];
  11.                                 bool bfind = false;
  12.                                 for (vector<CDR_CELL_EX>::iterator itcellex = vec_CellEx.begin(); itcellex != vec_CellEx.end(); itcellex++ )
  13.                                 {
  14.                                         if ( itcellex->stPilot.NE_SYS_ID == stPilotTemp.NE_SYS_ID )
  15.                                         {
  16.                                                 bfind = true;
  17.                                                 itcellex->nPilotCount++;
  18.                                                 break;
  19.                                         }
  20.                                 }
  21.                                 if (!bfind)
  22.                                 {
  23.                                         stCellExTemp.nPilotCount = 1;
  24.                                         stCellExTemp.stPilot = stPilotTemp;
  25.                                         vec_CellEx.push_back(stCellExTemp);
  26.                                 }
  27.                                
  28.                                 //map<__int64, CDR_CELL_EX>::iterator itfind = m_mapCellEx.find(stPilotTemp.NE_SYS_ID);
  29.                                 //if (itfind == m_mapCellEx.end()) //没有查找到,则增加一条
  30.                                 //{
  31.                                 //        m_mapCellEx[stPilotTemp.NE_SYS_ID].nCountValue = 1;
  32.                                 //        m_mapCellEx[stPilotTemp.NE_SYS_ID].stPilot = stPilotTemp;
  33.                                 //}
  34.                                 //else
  35.                                 //{
  36.                                 //        m_mapCellEx[stPilotTemp.NE_SYS_ID].nCountValue++;
  37.                                 //}
  38.                                
  39.                         }


  40. 上面如果使用map<__int64, CDR_CELL_EX> 把则需要对它的value进行排序

  41. http://www.cnblogs.com/wanpengcoder/archive/2010/10/24/1859682.html
  42. map默认是按照键(key)排序的。很多时候我们需要按值(value)排序,靠map里的

  43. 算法当然是不行的,那么可以把它转存到vector中,在对vector按照一定的规则排序即可。

  44. //示例代码:输入单词,统计单词出现次数并按照单词出现次数从多到少排序


  45. #include <cstdlib>

  46. #include <map>

  47. #include <vector>

  48. #include <string>

  49. #include <algorithm>

  50. #include <iostream>



  51. void sortMapByValue(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector);

  52. int cmp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y);



  53. int main()

  54. {

  55. std::map<std::string, int> tMap;

  56. std::string word;

  57. while (std::cin >> word)

  58. {

  59. std::pair<std::map<std::string, int>::iterator, bool> ret = tMap.insert(std::make_pair(word, 1));

  60. if (!ret.second)

  61. ++ret.first->second;

  62. }



  63. std::vector<std::pair<std::string,int> > tVector;

  64. sortMapByValue(tMap,tVector);

  65. for(int i=0;i<tVector.size();i++)

  66. {

  67.   std::cout<<tVector[i].first<<": "<<tVector[i].second<<std::endl;

  68. }  



  69. system("pause");

  70. return 0;

  71. }



  72. int cmp(const std::pair<std::string, int>& x, const std::pair<std::string, int>& y)

  73. {

  74. return x.second > y.second;

  75. }



  76. void sortMapByValue(std::map<std::string, int>& tMap, std::vector<std::pair<std::string, int> >& tVector)

  77. {

  78. for (std::map<std::string, int>::iterator curr = tMap.begin(); curr != tMap.end(); curr++)

  79. {

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

  81. }



  82. std::sort(tVector.begin(), tVector.end(), cmp);

  83. }

复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|firemail ( 粤ICP备15085507号-1 )

GMT+8, 2024-5-3 10:26 , Processed in 0.063530 second(s), 19 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表