C++ Primer 5th笔记(chap 11)关联容器---举例
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了C++ Primer 5th笔记(chap 11)关联容器---举例,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2883字,纯文字阅读大概需要5分钟。
内容图文
![C++ Primer 5th笔记(chap 11)关联容器---举例](/upload/InfoBanner/zyjiaocheng/605/ec748caa15294a06bd75a3d5b26af0b4.jpg)
1. code
#ifndef WORDTRANSFORM_H
#define WORDTRANSFORM_H
#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <string>
#include <stdexcept>
#include <sstream>
using std::map; using std::string; using std::vector;
using std::ifstream; using std::cout; using std::endl;
using std::getline;
using std::runtime_error; using std::istringstream;
class wordTransform {
public:
wordTransform(){}
virtual ~wordTransform() {}
map<string, string> buildMap(ifstream& map_file)
{
map<string, string> trans_map; // holds the transformations
string key; // a word to transform
string value; // phrase to use instead
// read the first word into key and the rest of the line into value
while (map_file >> key && getline(map_file, value))
if (value.size() > 1) // check that there is a transformation
trans_map[key] = value.substr(1); // skip leading space
else
throw runtime_error("no rule for " + key);
return trans_map;
}
const string&
transform(const string& s, const map<string, string>& m)
{
// the actual map work; this part is the heart of the program
auto map_it = m.find(s);
// if this word is in the transformation map
if (map_it != m.cend())
return map_it->second; // use the replacement word
else
return s; // otherwise return the original unchanged
}
// first argument is the transformations file;
// second is file to transform
void word_transform(ifstream& map_file, ifstream& input)
{
auto trans_map = buildMap(map_file); // store the transformations
// for debugging purposes print the map after its built
cout << "Here is our transformation map: \n\n";
for (auto entry : trans_map)
cout << "key: " << entry.first
<< "\tvalue: " << entry.second << endl;
cout << "\n\n";
// do the transformation of the given text
string text; // hold each line from the input
while (getline(input, text)) { // read a line of input
istringstream stream(text); // read each word
string word;
bool firstword = true; // controls whether a space is printed
while (stream >> word) {
if (firstword)
firstword = false;
else
cout << " "; // print a space between words
// transform returns its first argument or its transformation
cout << transform(word, trans_map); // print the output
}
cout << endl; // done with this line of input
}
}
void test() {
ifstream map_file("e:/temp/rules"); // open transformation file
if (!map_file) // check that open succeeded
throw runtime_error("no transformation file");
ifstream input("e:/temp/text"); // open file of text to transform
if (!input) // check that open succeeded
throw runtime_error("no input file");
word_transform(map_file, input);
}
};
#endif
2. 原始文件
rules:
brb be right back
k okay?
y why
r are
u you
pic picture
thk thanks!
l8r later
3. 输出
text:
where r u
y dont u send me a pic
k thk l8r
4. 结果
【引用】
- 代码
https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/wordTransform.h
内容总结
以上是互联网集市为您收集整理的C++ Primer 5th笔记(chap 11)关联容器---举例全部内容,希望文章能够帮你解决C++ Primer 5th笔记(chap 11)关联容器---举例所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。