要求:MFC中,选择一个文件(已经完成),读入里面的文章,文章包含大量的英语单词>5000,然后按下BUTTON(统计),在左边的ListBox中显示具体单词和出现次数
本人疑问:读入文章中的单词,存入数组(原始的没经过排序的,)排序我自己做好了,望达人赐教
本人疑问:读入文章中的单词,存入数组(原始的没经过排序的,)排序我自己做好了,望达人赐教
解决方案 »
- 高分,自己做了个串口调试软件,与C51单片机通信,有很多问题,持续更新中
- 请问如何解决出现大量的socket WSAECONNRESET(10054) 错误!
- 全国计算机编程大赛复赛题的一个问题
- 请问,我用CFileFind去访问一个局域网的共享目录,如果那台电脑关闭了,我的CFileFind会不会死等在那里?
- 大家帮帮忙(我想开始做个数据库的项目)
- 购买从winpcap抓的包中捕获分析到110,25邮件内容的程序的VC源代码
- 工具栏样式中TBSTYLE_TRANSPARENT 有什么用?
- 请问:LINK : fatal error LNK1104: cannot open file "odb.obj"是什么意思?
- 征求COM书籍,一本10分
- 线程池调度算法。?
- 关于内存问题!!!
- 关于CFormview的控件在主窗口给拖拉时闪烁的问题
建议建立一个map来存储单词于数量
可以考虑使用std:map或者set,自动就提供排序,也可以自定义排序.
简单点用你的单词作为关键字,映射到出现次数上面,读取文章的时候,每读取一个单词就检查是否已经存在,存在就增加出现次数,否则插入显示的时候直接遍历map就是已经顺序的了
可能对你有帮助的
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
#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
#pragma warning(disable:4786)
#include <map>
#include <string>
#include <iostream>
using namespace std;typedef map<string,int> MapWords;
你的程序我理解了。我先从读取文件开始我在我的程序中加入了以下代码用来读取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就报错
stl中,std::map实现为红黑树,而红黑树也属于二叉查找树,O(logn)的查找,删除,插入效率
是没有包含这个函数声明的头文件吧
#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或者当前操作类中都试过了
你把函数声明也放到stdafx.h里面就可以了
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;}
我这里有代码,需要在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)