首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

运用地图容器实现单词转换,求高手相助。

2012-10-19 
运用map容器实现单词转换,求高手相助。。。//用map容器实现单词的转换。//这个是C++primer上的程序,有好多错误

运用map容器实现单词转换,求高手相助。。。
//用map容器实现单词的转换。
//这个是C++primer上的程序,有好多错误,我是新手,改不过来,求高手相助。
#include<iostream>
#include<string>
#include<map>
#include<fstream>
#include<sstream>
using namespace std;
int main(int argc,char **argv)
{
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;
}
return 0;

}

[解决办法]

C/C++ code
#include<iostream>#include<string>#include<map>#include<fstream>#include<sstream>using namespace std;int main(int argc,char **argv){    map<string,string> trans_map;    string key,value;    if(argc != 3) //判断输入参数个数。        throw runtime_error("wrong number of arguments");    ifstream map_file;    map_file.open(argv[1]);    if(!map_file.is_open())        throw runtime_error("no transformation file");    while(map_file>>key>>value)        trans_map.insert (make_pair(key,value));    ifstream input;    input.open(argv[2]);    if (!input.is_open())        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;    }    return 0;}//你去修改一下你的文件,去建立一个就可以了
[解决办法]
不久之前 我就做了这个题目:
给我看看我自己写的:
[code=C/C++][/code]
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
int main(int argc,char **argv)
{
 
if (argc!=3)
{
throw runtime_error("参数错误");
}
 
ifstream findfile;
 ifstream transfile;
string f1=argv[1];
cout<<f1<<endl;
 string f2=argv[2];
 cout<<f2<<endl;
findfile.open(argv[1]);
 transfile.open(argv[2]);
if (!findfile&&!transfile)
{
cerr<<"open error"<<endl;
return -1;
}

map<string,string> findmap;
string str1,str2;
while(findfile>>str1>>str2)
findmap.insert(make_pair(str1,str2));
vector<string> str;
string s;
while (transfile>>s)
{
str.push_back(s);
}
cout<<"---------------"<<argv[2]<<"---------------"<<endl;
for (vector<string>::iterator it=str.begin();it!=str.end();it++)


{
cout<<*it<<endl;
}
cout<<endl;
cout<<str.size()<<"words"<<endl;

cout<<"trans result:"<<endl;

for (vector<string>::iterator tit=str.begin();tit!=str.end();tit++)
{
if (findmap.count(*tit)==1)
{
*tit=findmap[*tit];
cout<<*tit<<" ";
}
}
cout<<endl
<<"The End!!!"<<endl;

return 0;
}
[解决办法]
[Quote=引用:]

[Quote=引用:]

不久之前 我就做了这个题目:
给我看看我自己写的:
[code=C/C++][/code]
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
using namespace std;
int mai……
[/Quote]
貌似沒有錯誤啊,為何不把錯誤貼出來看看

热点排行