//读入一整行#include<iostream>#include<string>using std::string;using std::cin;using std::cout;using std::endl;int i =42;intmain(){
string line;while(getline(cin, line)){if(line.empty())break;
cout << line << endl;}return0;}
//读入一个词#include<iostream>#include<string>using std::string;using std::cin;using std::cout;using std::endl;int i =42;intmain(){
string world;while(cin >> world){//ending flag word "end"if(world =="end")break;
cout << world << endl;}return0;}
练习3.4
//字符串相等不相等#include<iostream>#include<string>using std::string;using std::cin;using std::cout;using std::endl;int i =42;intmain(){
string str1, str2;getline(cin, str1);getline(cin, str2);if(str1 == str2){
cout <<"str1 = str2"<< endl;}else{
string result{(str1 > str2)? str1 : str2 };
cout <<"the bigger one is: "<< result << endl;}return0;}
//字符串等长不等长#include<iostream>#include<string>using std::string;using std::cin;using std::cout;using std::endl;int i =42;intmain(){
string str1, str2;getline(cin, str1);getline(cin, str2);if(str1.size()== str2.size()){
cout <<"same length"<< endl;}else{
string result{(str1.size()> str2.size())? str1 : str2 };
cout <<"the bigger one is: "<< result << endl;}return0;}
练习3.6-练习3.11
练习3.14
#include<iostream>#include<vector>using std::vector;using std::cin;using std::cout;using std::endl;intmain(){
vector<int> vecint;int item;while(cin >> item){if(item ==-1)break;
vecint.push_back(item);}for(auto i : vecint) cout << i << endl;return0;}
练习3.15
#include<iostream>#include<vector>#include<string>using std::string;using std::vector;using std::cin;using std::cout;using std::endl;intmain(){
vector<string> vecstr;
string item;while(getline(cin, item)){if(item.empty())break;
vecstr.push_back(item);}for(auto i : vecstr) cout << i << endl;return0;}