1.从控制台 读入 用户输入的单个string
特征代码
string s;
cin>>s;
#include "stdafx.h" #include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter a string" << endl; cin >> s; cout << "You have entered : " << s << endl; system("pause"); return 0; }
2.从控制台 持续读入单个string 并进行字符串拼接
特征代码
while(cin>>s){}
#include<iostream> #include<string> using namespace std; int main() { string s, sNew; cout << "Please enter somestring" << endl; while (cin >> sNew) { s += sNew; cout << "You have entered : " << s << endl; } system("pause"); return 0; }
string读入的特点
1)从遇到的第一个不为空的字符串开始,即会忽略开头的空格
2)每遇到空格就停止本次读入
3)>>操作符返回读入的istream对象,即用户输入的内容,如果用户输入的内容不为空(达到文件为或遇到无效输入),那么会一直执行while循环
3.读取整行文本
getline(*in,string){}
作用:*in 输入流对象,如cin; 读入的字符串保存在string对象中
特点:1)遇到换行符即停止一次读入,即使开头就位换行符,那样的话,读入的字符串就为空
2)不会忽略开头的空格
返回值: 将不断读入的istream作为返回值,可以用于while的判断中(是否还有字符串输入)
#include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter string" << endl; while (getline(cin, s)) { cout << "You said:" << s<<endl; } system("pause"); return 0;
3.string的长度
概念:string包含的字符有多少个 如 c 1 空格
语句:string s;
s.size(); 作用:返回s包含的字符个数
返回类型 string::sizy_type型
可以直接输出s.size()
也可以用一个string::size_type类型的变量接收s.size()
string::size_type是unsigned类型中的一员
示例程序:
1)
#include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter the string:" << endl; cin >> s; cout << "You have entered: " << s << endl; cout << "The length of the string is: " << s.size()<< endl; system("pause"); return 0; }
2)
#include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter the string:" << endl; cin >> s; string::size_type size = s.size(); cout << "You have entered: " << s << endl; cout << "The length of the string is: " << size<< endl; system("pause"); return 0; }
注意 如果在s读入前就定义size=s.size();
则默认的size大小为0,读入后size也没有改变为字符串大小
?原因未知? 像这样:
3)
#include<iostream> #include<string> using namespace std; int main() { string s; string::size_type size = s.size(); cout << "Please enter the string:" << endl; cin >> s; cout << "You have entered: " << s << endl; cout << "The length of the string is: " << size<< endl; system("pause"); return 0; }
4)string size 包括空格
#include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter the string:" << endl; getline(cin,s); cout << "You have entered: " << s << endl; cout << "The length of the string is: " << s.size()<< endl; system("pause"); return 0; }
用getline 开头的空格会被读入string , size里包括空格字符
4.读取字符串中的字符
途径:通过下标获取
下标数据类型: size_type
示例一:判断字符串的开头字母是什么(联系OpenGL Object文件读入时判断数据类型)
#include<iostream> #include<string> using namespace std; int main() { string s; cout << "Please enter the string:" << endl; //string::size_type index; while (getline(cin, s)) { cout << "You have entered: " << s << endl; if (s[0] == 'v') cout << "This data is a vertex " << endl; else if (s[0] == 'f') cout << "This data is a face" << endl; else cout << "Dont't konw what is this." << endl; cout << "Please enter the string:" << endl; } system("pause"); return 0; }
示例二:
数一数一个字符串里有多少个字符x
#include<iostream> #include<string> using namespace std; int main() { string s; int num_x = 0; cout << "Please enter the string:" << endl; cin >> s; for (string::size_type i = 0; i < s.size(); i++) if (s[i] == 'x') num_x++; cout << "There are " << num_x << " \" x \" in the string." << endl; system("pause"); return 0; }