1.读取未知数量的string
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string world;
while(cin >> world)
{
cout << world << endl;
}
system("pause");
return 0;
}
2.使用getline读取一整行
while(getline(cin,line),line!="###")//重点
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string line;
while(getline(cin,line),line!="###")
{
cout << line << endl;
}
system("pause");
return 0;
}
3.string的size操作
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string line;
int a;
cin >> line;
//通过.size的操作即可计算出字符串的长度
// a = line.size();
// cout << "a=" << a << endl;
while(getline(cin,line))
{
if(line.size() > 80)
{
cout << line << endl;
}
else
cout << "码的巴卡" << endl;
break;
}
system("pause");
return 0;
}
4.string中的empty函数的使用
//empty函数根据string对象是否为空来返回一个对应的布尔值
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string line;
cin >> line;
//有问题!!!
// int a;
// a = line.empty();
// cout << a << endl;
while(getline(cin,line))
{
if(!line.empty())
cout << line << endl;
else
cout << "NO" << endl;
}
system("pause");
return 0;
}
5.两个string对象的相加
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string s1;
string s2;
cin >> s1 >> s2;
string s3 = s1+s2;
cout << s3 << endl;
cout << s1 << endl;
cout << s2 << endl;
system("pause");
return 0;
}
6.string与字面值相加
#include <iostream>
using namespace std;
#include <string>
int main()
{
system("color f5");
string s1;
cin >> s1;
string s2;
string s3;
s2 = s1+"hello";
//错的s3 = "hello"+",";
cout << s2 << endl;
cout << s3 << endl;
system("pause");
return 0;
}