游戏开发论坛

 找回密码
 立即注册
搜索
查看: 1371|回复: 0

map 排序 不是key值排序 编译不过 高手帮忙!

[复制链接]

2

主题

4

帖子

0

积分

新手上路

Rank: 1

积分
0
发表于 2008-4-13 22:23:00 | 显示全部楼层 |阅读模式
#include <iostream>
#include <fstream>
#include <string>
#include <algorithm>
#include <map>
#include <vector>
using namespace std;

int main ()
{
        ifstream InFile("article.txt");
        ofstream OutFile1("3_1_1out.txt");
        ofstream OutFile2("3_1_2out.txt");
       
        OutFile1<<"in lexicographic order:"<<endl;
        OutFile2<<"in descending frequency order:"<<endl;
       
       
        map<string,int> word_count;
    string  word;
       
        while(InFile>>word)
        {
                ++word_count[word];
        }
       

        map<string,int>::iterator map_it=word_count.begin();
       
        while(map_it!=word_count.end())
        {
                OutFile1 <<(*map_it).first << "\tEnter\t"
                        <<(*map_it).second<< "\ttimes "<<endl;
               
                map_it++;
        }


        typedef map<string,int >::value_type value_type;
    vector<value_type>temp;
    temp.reserve(word_count.size());
        copy(word_count.begin(),word_count.end(),back_inserter(temp));

        struct pred
        {
                bool operator()(const value_type& a, const value_type& b)
                {
                        return a.second<b.second;
                }

        }
        sort(temp.begin(),temp.end(),pred());
     

       
       
               
        return 0;
}



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

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-22 20:02

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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