单词转换

单词转换文件:

测试文件:

结果:

源代码:

#include<iostream>
#include<fstream>
#include<sstream>
#include<map>
using namespace std;
int main(void)
{
    ifstream file;                  //单词对应
    ifstream test;                  //测试用例
    file.open("file.txt",ios::in);
    test.open("test.txt",ios::in);
    if(!file)
    {
        cout<<"file not found or file can't open"<<endl;
    }
    if(!test)
    {
        cout<<"test not found or test can't open"<<endl;
    }
    string key,value;               //键值对
    map<string,string> m;           //map存贮
    while(file>>key>>value)
    {
        m.insert(make_pair(key,value));             //存入map
    }
    string line;
    string word;
    bool isFirst;
    map<string,string>::const_iterator it;
    while(getline(test,line))       //每次读入一行
    {
        istringstream iss(line);    //读取每个单词
        isFirst=true;
        while(iss>>word)
        {
            if((it=m.find(word))!=m.end())          //查找是否存在
            {
                word=it->second;
            }
            if(isFirst)             //如果是第一个
            {
                isFirst=false;
            }
            else                    //如果不是第一个
            {
                cout<<' ';
            }
            cout<<word;
        }
        cout<<endl;
    }
    file.close();
    test.close();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值