急,急,急!!!

xiaoxiubin 2007-04-13 09:12:49
以下一段程序是要统计文章单词的问题!,出不来结果,问题出在哪呢?请达人门指教
#include "stdafx.h"
#include <iostream>
#include <string>
#include <map>
#include <vector>
#include <fstream>
#include <algorithm>
#include <utility>
using namespace std;

#include <stddef.h>
#include <ctype.h>




int _tmain(int argc, _TCHAR* argv[])
{
ifstream infile("1.txt",ios::in);
if(!infile)
{ cerr<<"unable to open file"<<endl;
exit(-1);
}
else cout<<endl;
vector<string> *lines_of_text=new vector<string> ;
string textline;

while (getline(infile,textline,'\n'))
lines_of_text->push_back(textline);
vector<string>*words=new vector<string> ;

for(short line_pos=0;line_pos < lines_of_text->size();line_pos++)
{ short word_pos = 0;

string textline=(*lines_of_text)[line_pos];
string::size_type eol=textline.length();
string::size_type pos=0,prev_pos=0;
while((pos=textline.find_first_of(' ',pos))!= string::npos)
{
words->push_back(textline.substr(prev_pos,pos-prev_pos));

word_pos++; pos++;prev_pos=pos;
}
words->push_back(textline.substr(prev_pos,pos-prev_pos));
locations ->push_back(make_pair(line_pos,word_pos));
}

//去除标点符号;
string filt_elems( "\",.;:!<< \\/" );

vector<string>::iterator iter = words->begin();
while ( iter!=words->end() );
{
string::size_type pos1 = 0;
while (( pos1 = ( filt_elems, pos1 ))!= string::npos )
(*iter).erase(pos1,1);
++iter;
}
for(vector<string>::iterator iter1=words->begin();iter1!=words->end();iter1++)
cout<<*iter1<<endl;
return 0;

}
...全文
259 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
羊小丸子 2007-04-13
  • 打赏
  • 举报
回复
这样的题目应该很好改,先熟悉测试的工具和方式,然后直接调试跟踪,代码也不多应该很快就搞定了
pyrophile 2007-04-13
  • 打赏
  • 举报
回复
lz的代码好像把单词全部都给erase掉了
pyrophile 2007-04-13
  • 打赏
  • 举报
回复
看下面的代码:
while (iter != iter_end)
{
string::size_type pos = 0;
while ((pos = (*iter).find_first_of(filt_elems, pos)) != string::npos)
(*iter).erase(pos, 1);
++iter;
}
调试通过,lz结贴吧
orc1984 2007-04-13
  • 打赏
  • 举报
回复
调试, 单步, 设断点 跟踪进去一步一步 找到后
不会改再发到上面
xiaoxiubin 2007-04-13
  • 打赏
  • 举报
回复
string filt_elems( "\",.;:!<< \\/" );

vector<string>::iterator iter = words->begin();
while ( iter!=words->end() );
{
string::size_type pos1 = 0;
while (( pos1 = ( filt_elems, pos1 ))!= string::npos )
(*iter).erase(pos1,1);
++iter;
}
应该这段出问题了,可不知道在哪啊
taodm 2007-04-13
  • 打赏
  • 举报
回复
string::size_type pos1 = 0;
while (( pos1 = ( filt_elems, pos1 ))!= string::npos )
(*iter).erase(pos1,1);
看effective stl item 32,用erase+remove法
freshui 2007-04-13
  • 打赏
  • 举报
回复
编程一定要会调试, 单步 断点, watch 表达式....

这样才能找出错误
xiaoxiubin 2007-04-13
  • 打赏
  • 举报
回复
去标点前面程序都能出结果,就是去标点这一段程序,这段程序出错了
taodm 2007-04-13
  • 打赏
  • 举报
回复
上调试器,看你现在死循环在哪了。

65,213

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧