#include<iostream>
#include<string>
using namespace std;
int main() {
string ng,s; //字符串字面值与string是不同类型,若要相+,必须保证字面值的左边或右边必须有一个string类型
// if(cin>>ng)
// cout << ng.size() << endl;//.size返回ng中字符的个数(一个中文为2个字符),size()返回对象的长度如下
// if(ng.size() > 80); 如果ng内的字符大于80 cout<<ng<<endl; 输出ng
//size返回的是size_type类型,我的理解差不多是unsigned,最好不要与int等类型并存,也不要去比较大小
// if (ng.empty()) // .empty中文意思是"空的",若ng是一个空字符串,则执行,这里将执行下面的cout,empty返回布尔值,如果是空的返回True
//例如 if(!ng.empty()) !是逻辑非运算符,这行的意思是 如果(ng不是空的)
// cout << "ng是空的" << endl;
// getline(ng, s); //getline get一个line 就是第一行赋值给s
// cout << s << endl;
// cin >> ng >> endl; //用户输入,string会忽略空格,从第一个非空格开始读取,到最后一个空格终止,写入ng.例如 hello world,其实只写入了hello
//cin>>s1>>s2>>endl; 这样一次空格会分为s1和s2读取,例如 hello world,写入会是 helloworld(不带空格)
// while (cin >> ng) 循环输入,知道遇到结束符
// cout << ng << endl; 循环输出
while (getline(cin, ng)) //getline()带入参数cin,并且写入ng,getline为一整行,可以包含空格,因此空格不会被忽略,以换行符结束输入,换行符不写入ng
cout << ng << endl;
return 0;
}
3.22节练习:
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1, s2;
cin >> s1 >> s2;
if (s1 != s2) {
if (s1 > s2)
cout << s1 << endl;
else
cout << s2 << endl;
}
else
cout << s1 << " " << s2 << endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
string s1, s2;
cin >> s1 >> s2;
if (s1.size() != s2.size()) {
if (s1 > s2)
cout << s1<<"的长度是"<<s1.size() << endl;
else
cout << s2 <<"的长度是"<<s2.size()<< endl;
}
else
cout << s1 << " and " <<s2<<"一样长"<< endl;
return 0;
}
#include<iostream>
#include<string>
using namespace std;
int main() {
string x1,x2;
if (cin >> x1 >> x2)
cout << x1 << " " << x2 << endl;
return 0;
}