C++primer第十章,单词转换map对象,在VC6.0运行,出现debug异常,弹出终止,重试,忽略对话框。咋回事呀
C++primer第十章,单词转换map对象,在VC6.0运行,出现debug错误,弹出终止,重试,忽略对话框。怎么回事呀
编译无错误,但有127个warning。程序代码如下:
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include<sstream>
#include<iterator>
#include <stdexcept>
using namespace std;
void main(int argc,char **argv)
{
ifstream &open_file(ifstream &,const string &);
map <string,string> trans_map;
string key, value;
if(argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if(!(open_file(map_file,argv[1])))
throw runtime_error("no transformation file");
while (map_file>> key >>value)
trans_map.insert(make_pair(key,value));
ifstream input;
if(!(open_file(input,argv[2])))
throw runtime_error("no input file");
string line;
while(getline(input,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword=false;
else
cout<<" ";
cout<< word;
}
cout<<endl;
}
}
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
------解决方案--------------------
一般 是溢出错误 楼主调试下 看下问题出在哪
另外 楼主要看primer就不要用vc6.0 找罪受 很多悲剧 就是因为vc6.0发生的
不要懒 下个vs2010用吧
编译无错误,但有127个warning。程序代码如下:
#include <iostream>
#include <map>
#include <string>
#include <fstream>
#include<sstream>
#include<iterator>
#include <stdexcept>
using namespace std;
void main(int argc,char **argv)
{
ifstream &open_file(ifstream &,const string &);
map <string,string> trans_map;
string key, value;
if(argc != 3)
throw runtime_error("wrong number of arguments");
ifstream map_file;
if(!(open_file(map_file,argv[1])))
throw runtime_error("no transformation file");
while (map_file>> key >>value)
trans_map.insert(make_pair(key,value));
ifstream input;
if(!(open_file(input,argv[2])))
throw runtime_error("no input file");
string line;
while(getline(input,line))
{
istringstream stream(line);
string word;
bool firstword=true;
while(stream>>word)
{
map<string,string>::const_iterator map_it=trans_map.find(word);
if(map_it!=trans_map.end())
word=map_it->second;
if(firstword)
firstword=false;
else
cout<<" ";
cout<< word;
}
cout<<endl;
}
}
ifstream& open_file(ifstream &in,const string &file)
{
in.close();
in.clear();
in.open(file.c_str());
return in;
}
------解决方案--------------------
一般 是溢出错误 楼主调试下 看下问题出在哪
另外 楼主要看primer就不要用vc6.0 找罪受 很多悲剧 就是因为vc6.0发生的
不要懒 下个vs2010用吧