map 容器小例(hash map)
2015-12-09 20:32
288 查看
<span style="font-size:18px;">//导入两个文件t1存放映射规则,t2存放内容,将t2中的内容通过t1映射出来 /* t1 brb be right back k okay? y why r are u you pic picture thk thanks! 18r later */ /* t2 where r u y dont u send me a pic k thk 18r */ #include<iostream> #include<fstream> #include<sstream> #include<map> #include<unordered_map> //using namespace std; std::unordered_map<std::string,std::string> buildMap(std::ifstream &map_file); const std::string & transform(const std::string &s,const std::unordered_map<std::string,std::string>&m); void word_transform(std::ifstream &map_file,std::ifstream &input) { auto trans_map=buildMap(map_file); std::string text; while(getline(input,text)){ std::istringstream stream(text); std::string word; bool firstword=true; while(stream>>word){ if(firstword) firstword=false; else std::cout<<" "; std::cout<<transform(word,trans_map); } std::cout<<std::endl; } } std::unordered_map<std::string,std::string> buildMap(std::ifstream &map_file) { std::unordered_map<std::string,std::string>trans_map; std::string key; std::string value; while(map_file>>key&&getline(map_file,value)) if(value.size()>1) trans_map[key]=value.substr(1); else throw std::runtime_error("no rule for"+key); return trans_map; } const std::string & transform(const std::string &s,const std::unordered_map<std::string,std::string>&m) { auto map_it=m.find(s); if(map_it!=m.cend()) return map_it->second; else return s; } int main() { std::ifstream map_file,input; map_file.open("C:\\Users\\ACM\\Desktop\\t1.txt"); input.open("C:\\Users\\ACM\\Desktop\\t2.txt"); word_transform(map_file,input); return 0;</span> }
相关文章推荐
- Android 读取txt文件并以utf-8格式转换成字符串
- 动态规划 - 最长递增子序列(LIS)
- JPA(7) spring-data-jpa
- STL源码剖析之vector
- 升级oralce linux从7.1到7.2
- poj1823线段树
- 最小次数(递归)
- 枚举中关于toRaw()和fromRaw(3)编译出错
- UVALive 7279 Sheldon Numbers(位运算、暴力、想法)
- Codeforces 420 B. Online Meeting
- leetcode Path Sum II
- Android 读取txt文件并以utf-8格式转换成字符串
- 欢迎使用CSDN-markdown编辑器
- POJ 3252 Round Numbersm
- ubuntu下gerrit 安装部署
- sap JCO3.0安装缺少Microsoft Visual C++ 2005 Service Pack 1 Redistributable Package (KB973544)
- 106 Construct Binary Tree from Inorder and Postorder Traversal
- leetcode Flatten Binary Tree to Linked List
- gdb使用指南:单步执行和跟踪函数调用
- 谈WinDbg之InternalCall的使用与实现