C ++ Primer第5版:演習3.2 3.3 3.4 3.5

演習3.2

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

int main()
{
    
    
	string line;

	while (getline(cin, line))
		cout << line << endl;

	string word;

	while (cin >> word)
		cout << word << ' ';
	cout << endl;
}

演習3.3String
オブジェクトは、先頭の空白(つまり、スペースタブの改行など)を自動的に無視し、最初の文字から読み取ります。次の
getline関数で空白が検出されるまで、空白は自動的に無視され、条件として判断されません。関数の終わり

演習3.4

#include<iostream>
using namespace std;

int main()
{
    
    
	string s1 = "Hello", s2 = "World";

	if (s1 == s2)
		cout << "equal" << endl;
	else if (s1 > s2)
		cout << s1 << " bigger" << endl;
	else
		cout << s2 << "bigger" << endl;

	//modified
	if (s1.size == s2.size())
		cout << "same length" << endl;
	else
	{
    
    
		if (s1 == s2)
			cout << "equal" << endl;
		else if (s1 > s2)
			cout << s1 << " bigger" << endl;
		else
			cout << s2 << "bigger" << endl;
	}

}

演習3.5

#include<iostream>
using namespace std;

int main()
{
    
    
	string word;
	string conj;

	while (cin >> word)
		conj += word;
	cout << conj << endl;

	//modefied
	while (cin >> word)
		conj += " " + word;
	cout << conj << endl;
}

おすすめ

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