日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

c++ primer 5th 练习11.38自己编写答案(用无序容器重写单词计数程序)

發布時間:2024/4/18 c/c++ 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ primer 5th 练习11.38自己编写答案(用无序容器重写单词计数程序) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件main.cc?

#include <string> #include <iostream> #include <unordered_map> #include <fstream> #include "trans.h" using namespace std; int main() { ifstream read_map("map.txt"); ifstream read_file("file.txt"); trans(read_map,read_file);return 0; }

文件fun.cc

#include <iostream> #include <string> #include <unordered_map> #include <sstream> #include "trans.h" using namespace std; void trans(ifstream & standard_map,ifstream & is_file) { unordered_map<string,string> dictionary; dictionary = get_standard_dictionary(standard_map); string line; string word; while(getline(is_file,line)){//first_word要定義在這個地方,否則放在while循環的外部并初始化為true,第二行開始得每行多個空格bool first_word = true; //is也得定義在這個while內,如果定義在這個循環外,則整個循環只有一個內存流is,只能處理一行 istringstream is(line);while(is >> word){word = trans_line(dictionary,word);if(first_word)first_word = false;elsecout << " ";cout << word; }cout << endl;} }const unordered_map<string,string> get_standard_dictionary(ifstream & is) { unordered_map<string,string> um; string key,value; while(is>> key && getline(is,value)){if(value.size() > 1)um[key] = value.substr(1); elsethrow runtime_error("There is not mapped value about the key:" + key);}return um; }const string & trans_line(const unordered_map<string,string> & um,const string & line) { if(um.find(line) != um.end())return um.find(line)->second; elsereturn line; }

文件trans.h

#ifndef INCLUDE_H #define INCLUDE_H #include <string> #include <iostream> #include <map> #include <sstream> #include <fstream> #include <unordered_map> using namespace std; void trans(ifstream &,ifstream &); const unordered_map<string,string> get_standard_dictionary(ifstream &); const string & trans_line(const unordered_map<string,string> &,const string &);#endif

file.txt(存放需要轉換的單詞序列)

where r u y dont u send me a pic k thk l8r

map.txt

brb be right back k okay? y why r are u you pic picture thk thanks! l8r later

運行結果如下:

where are you why dont you send me a picture okay? thanks! later

?

總結

以上是生活随笔為你收集整理的c++ primer 5th 练习11.38自己编写答案(用无序容器重写单词计数程序)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。