C ++ Primer第5版:演習3.6 3.7 3.8 3.9 3.10 3.11

演習3.6

int main()
{
    
    
	string s = "Hello world!";

	for (auto& c : s)
		c = 'X';
	cout << s << endl;
}

演習3.7

自動キーワードタイプはcharであり、結果は変更されません。

演習3.8

int main()
{
    
    
	string s = "Hello world!";

	for (string::size_type i = 0; i != s.size(); ++i)
		s[i] = 'X';

	string::size_type i = 0;
	while (i != s.size())
		s[i++] = 'X';
}

forループの方が優れていて、直感的で便利だと思います

演習3.9
は不正であり、文字列はデフォルトで空の文字列として初期化され、添え字は範囲外です

演習3.10

#include<iostream>
#include<cctype>
using namespace std;

int main()
{
    
    
	string s = "Hello world!";

	for (auto& c : s)
		if (ispunct(c))
			c = ' ';
	cout << s << endl;
}

演習3.11は
合法であり、cのタイプはchar&
にアクセスできますが、値は変更できません

おすすめ

転載: blog.csdn.net/Xgggcalled/article/details/109009060