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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

c++ primer 5th,习题11.33

發布時間:2024/4/18 c/c++ 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c++ primer 5th,习题11.33 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文件trans.h

#ifndef INCLUDE_H #define INCLUDE_H#include <string> #include <iostream> #include <map> using namespace std;void trans(ifstream &map_trans,ifstream & words); map<string,string> standard_map(ifstream &); const string & transform(const map<string,string> &,const string &); void print_words(const map<string,string> &,const string &);#endif

文件main.cc

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

文件fun.cc

#include <string> #include <iostream> #include <fstream> #include <sstream> #include <map> #include "trans.h" using namespace std; void trans(ifstream &map_trans,ifstream & is_words) { //下面這個map_given必須是非const的,因為后面要給它賦值,const變量不能賦值。 //否則會報錯:passing "const std...等等" as 'this' argument discard qualifiers permissive //大題意思是,傳遞值得時候沒有考慮參數的允許,也就是類型不匹配 map<string,string> map_given; map_given = standard_map(map_trans); string line; while(getline(is_words,line)){print_words(map_given,line);cout << endl;} } void print_words(const map<string,string> & standard_map,const string &line) {istringstream is(line);string word;bool first_word = true;while(is >> word){if(first_word)first_word = false;elsecout << " ";cout << transform(standard_map,word); } }//下面的這個word是不是const的是無所謂的,只要傳遞給它的參數不是const的,他就可以不是const //但是如果word是const的,那么個函數返回值必須是const的,否則類型不匹配. const string & transform(const map<string,string> & m,const string &word){ //注:(1)下面的pos必須是const_iterator,否則會報錯。原因在于參數m是const的,所以它的 //迭代器也必須是const的 //(2)pos 必須定義在if條件的外部,不能定義在if后面的括號內(放條件判斷的那個),否則會報錯。 //因為定義語句保不定會返回什么。map<string,string>::const_iterator pos;if((pos = m.find(word)) != m.end())return pos->second;elsereturn word;}//下面這個standard_map的返回值可以是const也可以不是const的,因為這個返回值既不是引用也不是 //指針,所以不存在不存在底層不匹配問題.const map 類型也可以給 map類型賦值,所以他是不是const //對它的返回值使用沒影響。只要函數定義和trans.h文件中申明保持一致即可。 map<string,string> standard_map(ifstream & map_trans){map<string,string> self_map;string key;string value;istringstream is;while(map_trans >> key && getline(map_trans,value)){if(value.size() > 1)self_map[key] = value.substr(1);elsethrow runtime_error("No value about the key " + key); }return self_map;}

文件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.33的全部內容,希望文章能夠幫你解決所遇到的問題。

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