C++ Primer(第五版) 17.4.1--17.4.2节练习

本文介绍C++中如何使用标准库生成随机数,包括不同范围的整数随机数,并展示了如何构建和使用映射来转换文本。通过三个递进的随机数生成示例,以及一个复杂的文本转换函数,读者可以深入了解C++随机数模块和文本处理技巧。

17.28    

#include <iostream>
#include <random>

using namespace std;

unsigned random_val()
{
	static default_random_engine e;
	static uniform_int_distribution<unsigned> u(0,100);
	
	return u(e);
}

int main()
{
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << endl;
		
	return 0;
}

17.29    

#include <iostream>
#include <random>

using namespace std;

unsigned random_val(long seed = -1)
{
	static default_random_engine e;
	static uniform_int_distribution<unsigned> u(0,100);
	
	if (seed >= 0)
		e.seed(seed);
		
	return u(e);
}

int main()
{
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
		
	cout << random_val(5) << " ";
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
	
	cout << random_val(17823) << " ";
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
		
	return 0;
}

17.30

#include <iostream>
#include <random>

using namespace std;

unsigned random_val(long seed = -1, long min = 1, long max = 0)
{
	static default_random_engine e;
	static uniform_int_distribution<unsigned> u(0,100);
	
	if (seed >= 0)
		e.seed(seed);
		
	if (min <= max)
		u = uniform_int_distribution<unsigned>(min, max);
		
	return u(e);
}

int main()
{
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
		
	cout << random_val(5) << " ";
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
	
	cout << random_val(17823) << " ";
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
	
	cout << random_val(17823, 0, 9) << " ";
	for (auto i = 0; i < 10; ++i)
		cout << random_val() << " ";
	cout << endl;
		
	return 0;
}

17.31    在循环内定义b和e,每次循环都使用默认种子0初始化随机数引擎e,每次得到相同的随机数序列的第一个数;而在循环外定义,可保持引擎状态,每次得到随机数序列中的下一个值。

17.32    在循环内定义resp,其生命周期在do语句块内,不属于while条件判断,无法通过编译。

17.33    

#include <iostream>
#include <random>
#include <map>
#include <fstream>
#include <sstream>
#include <time.h>

using namespace std;

map<string, vector<string>> buildMap(ifstream &map_file)
{
	map<string, vector<string>> trans_map;
	string key;
	string value;
	while (map_file >> key && getline(map_file, value))
		if (value.size() > 1)
			trans_map[key].push_back(value.substr(1));
		else
			throw runtime_error("no rule for " + key);
	return trans_map;
}

const string&
transform (const string &s, const map<string, vector<string>> &m)
{
	static default_random_engine e(time(0));
	
	auto map_it = m.find(s);
	if (map_it != m.cend()) {
		uniform_int_distribution<unsigned> u(0, map_it->second.size() - 1);
		return map_it->second[u(e)];
	}		
	else
		return s;
}

void word_transform(ifstream &map_file, ifstream &input)
{
	auto trans_map = buildMap(map_file);
	string text;
	while (getline(input, text)) {
		istringstream stream(text);
		string word;
		bool firstword = true;
		while (stream >> word) {
			if (firstword)
				firstword = false;
			else 
				cout << " ";
			cout << transform(word, trans_map);
		}
		cout << endl;
	}
}

int main(int argc, char *argv[])
{
	ifstream m_file("mapfile.txt");
	ifstream input("infile.txt");
	
	word_transform(m_file, input);
	
	return 0;
	
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值