奇怪,问题何在?

newtocsdn 2004-11-14 07:52:09
当families.txt内无内容时候,可以编译、查询[当然,什么也差不到],但是,如果加入内容,奇怪,怎么就没有反应了???望指教......
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

typedef vector<string> vstring;
map<string,vstring> families;

//读取文件,并将内容置入map中
void populate_map(ifstream &nameFile,map<string,vstring> &families)
{
string textline;
while(getline(nameFile,textline))
{
string fam_name;
vector<string> child;
string::size_type pos=0,prev_pos=0,text_size=textline.size();

while(pos=textline.find_first_of(' ',pos)!=string::npos)
{
//计算子字符串的终点
string::size_type end_pos=pos-prev_pos;
//如果prev_pos未设值或其值为0,那么读到的单词就家庭姓氏
//否则就一一读取孩子的名字
if(!prev_pos)
fam_name=textline.substr(prev_pos,end_pos);
else
child.push_back(textline.substr(prev_pos,end_pos));
prev_pos=++pos;
}
//处理最后一个孩子的名字
//if(prev_pos<text_size)
// child.push_back(textline.substr(prev_pos,pos-prev_pos));
if ( prev_pos < text_size )
if( prev_pos )
//如果做过循环,则prev_pos不为0,就处理最後一个孩子
child.push_back(textline.substr(prev_pos,pos-prev_pos));
else
fam_name = textline.substr( prev_pos, pos - prev_pos );
//prev_pos为0,说明该家庭无孩子,处理家庭名

if(!families.count(fam_name))
families[fam_name]=child;
else
cerr<<"Oops! We already have a"
<<fam_name<<' '
<<"family in our map!\n";
}
}

//用户查询
void query_map(const string &family,const map<string,vstring> &families)
{
map<string,vstring>::const_iterator it=families.find(family);

if(it==families.end())
{
cout<<"Sorry. The"<<family<<' '
<<"is not currently entered.\n";
return ;
}

cout<<"The"<<family;

if(!it->second.size())
cout<<"has no children.\n";
else
{
//打印出vector内所有小孩的名字
cout<<"has"<<it->second.size()<<"children:";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
cout<<*iter++<<" ";
++iter;

}
cout<<endl;
}
}

//显示map内容
void display_map(const map<string,vstring> &families,ostream &os)
{
map<string,vstring>::const_iterator it=families.begin(),end_it=families.end();

while(it!=end_it)
{
os<<"The"<<it->first<<"family";
if(it->second.empty())
os<<"has no children.\n";
else
{
//打印出vector内所有小孩的名字
os<<"has"<<it->second.size()<<"children: ";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
os<<*iter<<" ";
++iter;

}
os<<endl;

}
++it;
}
}

int main()
{
map<string,vstring> families;
ifstream nameFile("F:\\Test\\families.txt");

if(!nameFile)
{
cerr<<"Unable to find families.txt file.Bailing out! \n";
return -1;
}

populate_map(nameFile,families);

string family_name;
while(1)
{
cout<<"Please enter a family name or q to quit:";
cin>>family_name;

if(family_name=="q")
break;
query_map(family_name,families);

}
display_map(families,cout);

system("pause");
return 0;
}

...全文
283 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
newtocsdn 2004-11-19
  • 打赏
  • 举报
回复
重新编译通过!
奇怪
有时候VC6.0会出现莫名的错误
哎!这个是为什么呀???
代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

typedef vector<string> vstring;
map<string,vstring> families;

//读取文件,并将内容置入map中
void populate_map(ifstream &nameFile,map<string,vstring> &families)
{
string textline;
while(getline(nameFile,textline))
{
string fam_name;
vector<string> child;
string::size_type pos=0,prev_pos=0,text_size=textline.size();

// while((pos = textline.find_first_of(' ', prev_pos)) != string::npos)
while((pos = textline.find_first_of(' ', pos)) != string::npos)
{
//计算子字符串的终点
string::size_type end_pos=pos-prev_pos;
//string::size_type end_pos=string::npos;
//while((pos = textline.find_first_of(' ', prev_pos)) != string::npos)

//如果prev_pos未设值或其值为0,那么读到的单词就家庭姓氏
//否则就一一读取孩子的名字
if(!prev_pos)
fam_name=textline.substr(prev_pos,end_pos);
else
child.push_back(textline.substr(prev_pos,end_pos));
prev_pos=++pos;
}
//处理最后一个孩子的名字
//if(prev_pos<text_size)
// child.push_back(textline.substr(prev_pos,pos-prev_pos));
if (prev_pos<text_size)
if(prev_pos)
//如果做过循环,则prev_pos不为0,就处理最後一个孩子
child.push_back(textline.substr(prev_pos,pos-prev_pos));
else
fam_name = textline.substr(prev_pos,pos-prev_pos);
//prev_pos为0,说明该家庭无孩子,处理家庭名
if(!families.count(fam_name))
families[fam_name]=child;
else
cerr<<"Oops! We already have a: " <<fam_name<<' '<< "family in our map!\n";
}
}

//用户查询
void query_map(const string &family,const map<string,vstring> &families)
{
map<string,vstring>::const_iterator it=families.find(family);

if(it==families.end())
{
cout<<"Sorry. The #"<<family<<' '
<<"is not currently entered.\n";

return ;
}

cout<<"The"<<family;

if(!it->second.size())
cout<<"has no children.\n";
else
{
//打印出vector内所有小孩的名字
cout<<"has #"<<it->second.size()<<' '<<"children:";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
cout<<*iter<<" ";
++iter;
}

cout<<endl;
}
}

//显示map内容
void display_map(const map<string,vstring> &families,ostream &os)
{
map<string,vstring>::const_iterator it=families.begin(),end_it=families.end();

while(it!=end_it)
{
os<<"The #"<<it->first<<' '<<"family";
if(it->second.empty())
os<<" has no children.\n ";
else
{
//打印出vector内所有小孩的名字
os<<" has #"<<it->second.size()<<' '<<"children:";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
os<<*iter<<" ";
++iter;
}
os<<endl;
}
++it;
}
}


int main()
{
map<string,vstring> families;
ifstream nameFile("F:\\C++\\Test\\3_3\\3_3\\families.txt");

if(!nameFile)
{
cerr<<"Unable to find families.txt file.Bailing out! \n";
return -1;
}

string family_name;
populate_map(nameFile,families);

while(1)
{
cout<<"Please enter a family name or q to quit:";
cin>>family_name;

if(family_name=="q")
break;

query_map(family_name,families);
display_map(families,cout);
}
// display_map(families,cout);
system("pause");
return 0;
}

newtocsdn 2004-11-17
  • 打赏
  • 举报
回复
To y8t47h(歪八):
题目是定义一个map,以家庭姓氏为key,value则是家庭所有小孩的名字,允许用户根据姓氏来查询,并得以打印map内的每一笔数据。

以populate_map()读取文件,并将内容置于map中:
void populate_map(ifstream &nameFile,map<string,vstring> &families)
{
...//个人认为这里涉及有问题 但是有没有好的解决方案 在看看ing
}

定义一个display_map()函数,显示map内容:
void display_map(const map<string,vstring> &families,ostream &os)
{
...
}

定义一个用户查询函数query_map():
void query_map(const string &family,const map<string,vstring> &families)
{
...
}


y8t47h 2004-11-17
  • 打赏
  • 举报
回复

希望能大体描述一下的程序,

一大堆代码,

很费眼睛的。
newtocsdn 2004-11-17
  • 打赏
  • 举报
回复
呵呵
刚才稍微修改了一下
但是出现了一个错误:fatal error C1063:Error executing cl.exe.
这个怎么解决呀???
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>

using namespace std;

typedef vector<string> vstring;
map<string,vstring> families;

//读取文件,并将内容置入map中
void populate_map(ifstream &nameFile,map<string,vstring> &families)
{
string textline;
while(getline(nameFile,textline))
{
string fam_name;
vector<string> child;
string::size_type pos=0,prev_pos=0,text_size=textline.size();

while((pos = textline.find_first_of(' ', prev_pos)) != string::npos)
{
//计算子字符串的终点
//string::size_type end_pos=pos-prev_pos;
string::size_type end_pos=string::npos;
//from while((pos = textline.find_first_of(' ', prev_pos)) != string::npos)

//如果prev_pos未设值或其值为0,那么读到的单词就家庭姓氏
//否则就一一读取孩子的名字
if(!prev_pos)
fam_name=textline.substr(prev_pos,end_pos);
else
child.push_back(textline.substr(prev_pos,end_pos));
prev_pos=++pos;
}
//处理最后一个孩子的名字
//if(prev_pos<text_size)
// child.push_back(textline.substr(prev_pos,pos-prev_pos));
if (prev_pos<text_size)
if(prev_pos)
//如果做过循环,则prev_pos不为0,就处理最後一个孩子
child.push_back(textline.substr(prev_pos,pos-prev_pos));
else
fam_name = textline.substr(prev_pos,pos-prev_pos);
//prev_pos为0,说明该家庭无孩子,处理家庭名
if(!families.count(fam_name))
families[fam_name]=child;
else
cerr<<"Oops! We already have a: " <<fam_name<<' '<< "family in our map!\n";
}
}

//用户查询
void query_map(const string &family,const map<string,vstring> &families)
{
map<string,vstring>::const_iterator it=families.find(family);

if(it==families.end())
{
cout<<"Sorry. The"<<family<<' '
<<"is not currently entered.\n";
return ;
}

cout<<"The"<<family;

if(!it->second.size())
cout<<"has no children.\n";
else
{
//打印出vector内所有小孩的名字
cout<<"has"<<it->second.size()<<' '<<"children:";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
cout<<*iter<<" ";
++iter;
}

cout<<endl;
}
}

//显示map内容
void display_map(const map<string,vstring> &families,ostream &os)
{
map<string,vstring>::const_iterator it=families.begin(),end_it=families.end();

while(it!=end_it)
{
os<<"The "<<it->first<<" family ";

if(it->second.empty())
os<<" has no children.\n ";
else
{
//打印出vector内所有小孩的名字
os<<" has "<<it->second.size()<<" children: ";
vector<string>::const_iterator iter=it->second.begin(),end_iter=it->second.end();

while(iter!=end_iter)
{
os<<*iter<<" ";
++iter;
}

os<<endl;

}
++it;
}
}


int main()
{
map<string,vstring> families;
ifstream nameFile("families.txt");

if(!nameFile)
{
cerr<<"Unable to find families.txt file.Bailing out! \n";
return -1;
}

populate_map(nameFile,families);
display_map(families,cout);

string family_name;
while(1)
{
cout<<"Please enter a family name or q to quit:";
cin>>family_name;

if(family_name=="q")
break;
query_map(family_name,families);
}

display_map(families,cout);

system("pause");
return 0;
}
newtocsdn 2004-11-17
  • 打赏
  • 举报
回复
晕~~
现在还没有解决的途径
望大虾指点
察看代码ing

Andy84920 2004-11-16
  • 打赏
  • 举报
回复
下面这个函数改成这个样子试一下:

void populate_map(ifstream &nameFile,map<string,vstring> &families)
{
string textline;
while(getline(nameFile,textline))
{
string fam_name;
vector<string> child;
string::size_type pos=0,prev_pos=0,text_size=textline.size();

while((pos=textline.find_first_of(' ',pos))!=string::npos) //
{
//计算子字符串的终点
string::size_type end_pos;
end_pos=pos-prev_pos;
//如果prev_pos未设值或其值为0,那么读到的单词就家庭姓氏
//否则就一一读取孩子的名字
if(!prev_pos)
fam_name=textline.substr(prev_pos,end_pos);
else
child.push_back(textline.substr(prev_pos,end_pos));
prev_pos=++pos;
}
//处理最后一个孩子的名字
//if(prev_pos<text_size)
// child.push_back(textline.substr(prev_pos,pos-prev_pos));
if ( prev_pos < text_size )
if( prev_pos )
//如果做过循环,则prev_pos不为0,就处理最後一个孩子
child.push_back(textline.substr(prev_pos,pos-prev_pos));
else
fam_name = textline.substr( prev_pos, pos - prev_pos );
//prev_pos为0,说明该家庭无孩子,处理家庭名

if(!families.count(fam_name))
families[fam_name]=child;
else
cerr<<"Oops! We already have a"
<<fam_name<<' '
<<"family in our map!\n";
}
}

另:你的程序写的太复杂化了.我只是改了一个小地方,不知道是否正确,反正能循环就是了.你再看看,
如果和你没有内容时不一样那发短消息给我.(不然我会忘记的).如果输出不对,那就是你的程序处理的问题了.
newtocsdn 2004-11-16
  • 打赏
  • 举报
回复
呵呵
是呀
输入查询的时候确实有问题
void populate_map(ifstream &nameFile,map<string,vstring> &families)
设计有问题!!!
本来是练习一下
看来问题不少呢
Andy84920 2004-11-16
  • 打赏
  • 举报
回复
唉,输入查询是有问题...
beyondtkl 2004-11-16
  • 打赏
  • 举报
回复
??
还是看你的逻辑处理 有没有OK...
Andy84920 2004-11-16
  • 打赏
  • 举报
回复
你说有死循环?我这里没有啊,我的文件内容是:
ff ff

Please enter a family name or q to quit:q
Thefffamilyhas1children: ff
请按任意键继续 . . .
newtocsdn 2004-11-15
  • 打赏
  • 举报
回复
呵呵
谢谢
Oops,Anyone responses?
xuzheng318 2004-11-14
  • 打赏
  • 举报
回复
1楼 帮你顶!

64,654

社区成员

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

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