ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++ Primer 5th笔记(chap 11)关联容器---举例

2021-02-12 15:01:25  阅读:182  来源: 互联网

标签:11 map word 5th --- std file using include


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. 结果
在这里插入图片描述

【引用】

  1. 代码
    https://github.com/thefistlei/cplusprimer/blob/main/cprimer/cprimer/wordTransform.h

标签:11,map,word,5th,---,std,file,using,include
来源: https://blog.csdn.net/thefist11cc/article/details/113794850

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有