3.2
Write a program to read the standard input a line at a time.
Modify your program to read a word at a time.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str_in;
while(getline(cin,str_in))
cout << str_in << endl;
return 0;
}
Modified version:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str_in;
while(cin >> str_in)
cout << str_in << endl;
return 0;
}
3.3
Explain how whitespace characters are handled in the string input operator and in the getline function.
对于string的输入运算符(>>)
忽略开头的空白,从第一个非空白字符开始读起,直到遇到下一处空白为止。
对于getline()
读取字符(包括除换行符之外的空白)直到遇到第一个换行符,对于换行符,只读不取。
3.4
Write a program to read two strings and report whether the strings are equal. If not, report which of the two is larger.
Now, change the program to report whether the strings have the same length, and if not, report which is longer.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str1,str2;
cin >> str1 >> str2;
if(str1 == str2)
cout << "两个字符串相等。";
else
{
cout << "两个字符串不相等,"
<< "且较大的字符串是:\n";
if(str1 > str2)
cout << str1;
else
cout << str2;
}
cout << endl;
return 0;
}
changed version:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str1,str2;
cin >> str1 >> str2;
auto size1 = str1.size(), size2 = str2.size();
if(size2 == size1)
cout << "两个字符串的长度相等。";
else
{
cout << "两个字符串的长度不相等,"
<< "且较长的字符串是:\n";
if(size1 > size2)
cout << str1;
else
cout << str2;
}
cout << endl;
return 0;
}
3.5
Write a program to read strings from the standard input, concatenating what is read into one large string. Print the concatenated string.
Next, change the program to separate adjacent input strings by a space.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str_in, str;
while(cin >> str_in)
str = str + str_in;
cout << endl << str << endl;
return 0;
}
Changed version:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str_in, str;
while(cin >> str_in)
str = str + str_in + ' ';
cout << endl << str << endl;
return 0;
}
3.6
Use a range for to change all the characters in a string to X.
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string str;
cin >> str;
for(auto &ch:str)
ch = 'A';
cout << str << endl;
return 0;
}
3.7
What would happen if you define the loop control variable in the previous exercise as type char? Predict the results and then change your program to use a char to see if you were right.
str中的字符不会被转换为X。
因为若要通过范围for改变string中字符的值,须把循环变量定义为引用类型。
若非定义为引用类型,而是定义为char, 则循环变量只是依次被初始化为string中的各个字符,改变循环变量对原始string无影响。
3.8
Rewrite the program in the first exercise, first using a while and again using a traditional for loop. Which of the three approaches do you prefer and why?
while loop:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s;
cin >> s;
decltype(s.size()) i = 0;
while(!s.empty() && i<s.size())
{
s[i] = 'A';
i++;
}
cout << s << endl;
return 0;
}
traditional for loop:
#include<iostream>
#include<string>
using namespace std;
int main(void)
{
string s;
cin >> s;
for(decltype(s.size()) i = 0;
!s.empty() && i<s.size(); i++)
s[i] = 'A';
cout << s << endl;
return 0;
}
更喜欢用范围for. 因为可以避免操作索引。
3.9
What does the following program do? Is it valid? If not, why not?
string s;
cout << s[0] << endl;
不合法,因为s为空,因此对s[0]的访问结果未定义。