C++Primer_课后习题3.23节

时间:2020-04-25
本文章向大家介绍C++Primer_课后习题3.23节,主要包括C++Primer_课后习题3.23节使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

本人是一个小菜鸟,最近才开始学习的C++,这个是我自己写的课后习题

版本是第五版

3.2.3节课后练习

练习3.6

//3.23节练习
//练习3.6
#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl;
int main()
{	
	string str("I will become a better man !");
	for (auto &c : str)
	{
		c = 'X';
	}
	cout << str << endl;
	cout << "\n";				//这个是换行符
	system("pause");			//这个是系统的暂停,方便观察结果	
	return 0;
}

所有的字符,是包括空格的(因为空格也算一个字符)

下面是程序结果

练习3.7

//3.23节练习
//练习3.7
#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl;
int main()
{	
	string str("I will become a better man !");
	for (char  &c : str)		//两题里面唯一更改的地方
	{
		c = 'X';
	}
	cout << str << endl;
	cout << "\n";				//这个是换行符
	system("pause");			//这个是系统的暂停,方便观察结果	
	return 0;
}

结果

也还是一样的,我认为,for(declaration :expression )范围语句declaration是变量的意思,expression是表达式的意思

for (char  &c : str)		
	{
		c = 'X';
	}

就相当于是c是每一个str里的字符的引用。于第一题差不多,不过3.6还用用auto判断.

练习3.8

//练习3.8
#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl;
int main()
{
	string str("I will become a better man !");
	for (decltype(str.size()) index = 0; index != str.size(); index++)
	{
		str[index] = 'x';
	}
	cout << str << endl;
	cout << "\n";				
	system("pause");
	return 0;
}

结果

和上题一样

练习3.9

#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl;
int main()
{
	string s;
	cout << s[0] << endl;
	system("pause");
	return 0;
}

结果

结果很奇怪,没有报错,去网上搜了搜,这个是未定义的结果,就是什么都可能,只不过我的这个是换行符。书中对这个行为也有着明确的解释。

联系3.10

#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl; using std::cin;
int main()
{
	string word;
	while (cin >> word)
	{
		cout << word << endl;  //我们输入的字符串
		for (auto &s : word)
			if (s != ',' && s != '.' && s != '!' && s != '"' && s != ';' && s != '\'' && s != '\"' && s != '+' && s != '-' && s != '/' && s != '\?' && s != '!' && s != '\\' && s != '*'&& s!='[' && s!=']')
				//依次分别是, . ! " : ' " + - / ? ! \ * 【 】 一些基本的标点符号  其中部分用到了转义字符(不是完整的符号,完整的符号有点小多)
				cout << s;//更改后的字符,我这里输出的是字符
	}
	//cout << word << endl;
	
	cout << "\n";
	system("pause");
	return 0;
}

我写的输入是while输入,有写特性

1.它一不会跳出,就是你可以无限的写

2.字符串里不能有空格,有空格就会被认为是2个字符

一个字符的时候

这个代码是不可以输入空格的,输入空格就会出错

第二种方法

使用getline()

#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl; using std::cin;
int main()
{
	string word;
	while (getline(cin,word))
	{
		cout << word << endl;  //我们输入的字符串
		for (auto &s : word)
			if (s != ',' && s != '.' && s != '!' && s != '"' && s != ';' && s != '\'' && s != '\"' && s != '+' && s != '-' && s != '/' && s != '\?' && s != '!' && s != '\\' && s != '*'&& s!='[' && s!=']')
				//依次分别是, . ! " : ' " + - / ? ! \ * 【 】 一些基本的标点符号  其中部分用到了转义字符(不是完整的符号,完整的符号有点小多)
				cout << s;//更改后的字符,我这里输出的是字符
	}
	//cout << word << endl;
	
	cout << "\n";
	system("pause");
	return 0;
}

练习3.11

//练习3.11
#include<string>
#include<iostream>
using std::string; using std::cout; using std::endl;
int main()
{
	const string s = "Keep out !";
	for (auto &c : s)
		cout << c; //这个是为了测试的语句
	cout << "\n";
	system("pause");
	return 0;
}

代码可以运行,说明这种范围for合法,c的类型经我测试加猜想,一应该是字符型。

原文地址:https://www.cnblogs.com/a-small-Trainee/p/12772724.html