要求:MFC中,选择一个文件(已经完成),读入里面的文章,文章包含大量的英语单词>5000,然后按下BUTTON(统计),在左边的ListBox中显示具体单词和出现次数
本人疑问:读入文章中的单词,存入数组(原始的没经过排序的,)排序我自己做好了,望达人赐教

解决方案 »

  1.   

    读入的内容,可以用空格与回车换行区分,
    建议建立一个map来存储单词于数量
      

  2.   

    读取文章的时候不就可以统计了么?
    可以考虑使用std:map或者set,自动就提供排序,也可以自定义排序.
    简单点用你的单词作为关键字,映射到出现次数上面,读取文章的时候,每读取一个单词就检查是否已经存在,存在就增加出现次数,否则插入显示的时候直接遍历map就是已经顺序的了
      

  3.   

    msdn中std:map参考,每个方法都有例子
      

  4.   

    我给你看看我一个以前写的程序啊
    可能对你有帮助的
    http://lg-hou.home.sunbo.net/xfile.php?xname=G13JA01&fname=/%B4%CA%B7%A8%D3%EF%B7%A8%D3%EF%D2%E5%B7%D6%CE%F6%B3%CC%D0%F2.rar&download=1
      

  5.   

    是在把单词怎么样放到数组里,因为单词数量多。怎么样判断是个单词我知道,是不是一定要用MAP,或者说用了MAP能带来很大便利,如果是,就帮忙用个小程序或者一段代码解释下,谢谢
      

  6.   

    最简单的一个:#pragma warning(disable:4786)
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;typedef map<string,int> MapWords;void AddWord(MapWords & words,const string & word);
    void PrintWords(MapWords & words);
    int main(int argc, char* argv[])
    {
    MapWords words;
    AddWord(words,"abc");
    AddWord(words,"abcd");
    AddWord(words,"abc");
    AddWord(words,"abcd");
    AddWord(words,"abc"); PrintWords(words);
    return 0;
    }void AddWord(map<string,int> & words,const string & word)
    {
    MapWords::iterator iter = words.find(word);
    if( iter == words.end() )
    {
    words.insert(MapWords::value_type(word,1));
    }
    else
    {
    iter->second += 1;
    }
    }void PrintWords(MapWords & words)
    {
    for(MapWords::iterator iter = words.begin(); iter != words.end(); ++iter)
    {
    cout<<iter->first<<":"<<iter->second<<endl;
    }
    }输出结果:
    abc:3
    abcd:2
      

  7.   

    不好意思,昨天断网了。有个问题,这些放进MFC需要做哪些处理,大概?主要下面几句
    #pragma warning(disable:4786)
    #include <map>
    #include <string>
    #include <iostream>
    using namespace std;typedef map<string,int> MapWords;
      

  8.   


    你的程序我理解了。我先从读取文件开始我在我的程序中加入了以下代码用来读取Text:
    vector<string> *text_file = retrieve_text(m_FileName);vector<string> *retrieve_text(string file_name){       ifstream artcile_file( file_name.c_str(), ios::in );       if (!artcile_file) {              cout << "Conn't open " << file_name.c_str() << " !" << endl;              exit (1);       }       vector<string> *lines_of_text = new vector<string>;       string textline;         while ( getline(artcile_file, textline, '\n'))       {              //cout << "    " << textline << '\n';              lines_of_text->push_back(textline);       }       return lines_of_text;}{}中的代码我暂时没写,上面这个是我从一个C++的关于这个方面的一个例子中看到的,觉得是应该这样,但为什么出现以下错误
    D:\程序\Stat\StatDlg.cpp(117) : error C2065: 'retrieve_text' : undeclared identifier
    D:\程序\Stat\StatDlg.cpp(117) : error C2440: 'initializing' : cannot convert from 'int' to 'class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,s
    truct std::char_traits<char>,class std::allocator<char> > > > *'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    Error executing cl.exe.Stat.exe - 2 error(s), 0 warning(s)
    但只在C++下是可以的,放入MFC就报错
      

  9.   


    stl中,std::map实现为红黑树,而红黑树也属于二叉查找树,O(logn)的查找,删除,插入效率
      

  10.   

    retrieve_text,没有声明,
    是没有包含这个函数声明的头文件吧
      

  11.   

    把 retrieve_text的声明放到你的头文件中声明一下
      

  12.   

    #pragma warning(disable:4786)
    #include <string>
    #include <iostream>
    #include <fstream>
    #include <functional>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <set>
    using namespace std;typedef map<string, int>::value_type sival_type;
    这些啊,空行前面的我都放到stdafx里了,后面一句我放到stdafx或者当前操作类中都试过了
      

  13.   

    不是这个问题,它是说你的retrieve_text函数没有声明
    你把函数声明也放到stdafx.h里面就可以了
      

  14.   

    跟retrieve_text有关的就这个了
    vector<string> *text_file = retrieve_text(m_FileName);vector<string> *retrieve_text(string file_name){       ifstream artcile_file( file_name.c_str(), ios::in );       if (!artcile_file) {              cout << "Conn't open " << file_name.c_str() << " !" << endl;              exit (1);       }       vector<string> *lines_of_text = new vector<string>;       string textline;         while ( getline(artcile_file, textline, '\n'))       {              //cout << "    " << textline << '\n';              lines_of_text->push_back(textline);       }       return lines_of_text;}
      

  15.   

    终于能用了,继续继续
    我这里有代码,需要在MFC下实现,能帮忙能够试试么?http://dev.csdn.net/develop/article/47/47835.shtm头文件都已经添加stdafx中了,然后
    void CStatDlg::OnStatFile() 
    {
    // TODO: Add your control notification handler code here
    if (m_FileName.IsEmpty())
    {
    ::AfxMessageBox("请选择文件.");
    return;
    }
        vector<string> *text_file = retrieve_text(m_FileName);}vector<string> *retrieve_text(string file_name){       ifstream artcile_file( file_name.c_str(), ios::in );       vector<string> *lines_of_text = new vector<string>;
           string textline;        while ( getline(artcile_file, textline, '\n'))       {
                  lines_of_text->push_back(textline);
           }
           return lines_of_text;}m_FileName为我取文件的路径,但出现以下状况
    Compiling...
    StatDlg.cpp
    D:\程序\Stat\StatDlg.cpp(117) : error C2660: 'retrieve_text' : function does not take 2 parameters
    Error executing cl.exe.Stat.exe - 1 error(s), 0 warning(s)