C++ 标准库函数string门类要点总结
String 对象的定义和初始化
String对象的读写和基本操作
①iostream标准库读写:cin>>s;
读取规则:从标准输入读取string,并存入串s中。
| -- 读取并忽略开头的空白字符
| -- 读取直至再次遇到空白字符为止。
例如:输入 hello world ; 串s值为hello
读取未知数目的string对象
string word; while(cin>> word) cout<<word<<endl;
②getline读取整行文本
while(getline(cin,word)) cout<<word<<endl;
getline不忽略开头和结尾的空白字符,它只要遇到换行符(即使在开头也一样)停止读入并返回(不读入换行符)。换-行符不存储在string对象中。
③string对象的操作
|-- s.empty() 判断字符串是否为空(成员函数)
|-- s[n] 返回s中位置为n的字符,位置从0开始计数
|-- s1 + s2 链接字符串
|-- s1 = s2 将s1替换成s2的副本
|-- s1 == s2 比较s1和s2的内容,如果相同,返回true
|-- !=, < , <=,>, >= 保持其原有意思
string str1;
str2 = "hello"
str1 = str2;
它必须先释放掉是str1占用的相关内存,然后分配给str1足够存放str2副本的内存空间,最后把str2中的所有字符复制到新分配的内存空间。
**********当进行string对象和字符串字面值混合连接时,+操作符的左右操作数至少有一个是string类型的。(连接方向从左至右)
实例: string s1 = "Hello"; string s2 = "world" ;
string s3 = s1 + ", " + "world" ; // 正确
string s4 = "Hello" + ", " + s2 ; // 错误
********string::size_type类型,注意:string对象操作中的size()返回的值不是int型的,而是size_type()类型的;任何存储string的size操作结果的变量必须是string::size_type类型的,特别重要的是,不要将size的返回值赋给整型类型。string::size_type类型在不同的机器上长度是不同的,并非是固定的,但是只要你使用了这个类型,就使你的程序适合这个机器,与实际机器匹配。但是使用int类型的时候,如果你的string:size_type长度刚好和int类型匹配,那么结果正确,但是如果二者不相同(比如换平台了),那么结果就完全不一样了(具体例子可以看百度百科:http://baike.baidu.com/link?url=u-bqQRoPPGzAkDetbzEZQCdhwVDoAr_YrGk6NiRbJF6dCOMkxcGwaWdKvy6m9ydhfq4TlsLtRGs-bVDirYBpy_)
********* == 比较两个string对象是否相等,相等指的是:长度相同,且含有相同字符。
********* 关系操作符用于测试两个string对象的大小关系,比较时采用了和(大小写敏感)字典排序相同的策略
实例: “Hiyya” > "Hello" "Hello" < "Hello World!" .
********下表操作:string对象下标是从0开始的,若为空则不含s[0]。
① 获取每个字符
string s1 = "hello world"; for(string::size_type i=0;i<=(s1.size()-1);i++){ cout<<s1[i]<<endl; }
②下表作为左值:
string s1 = "hello world"; s1[0]='*'; cout<<s1<<endl; //"*ello world"
③任何可产生整型值的表达式都可以作为下标操作符索引。(不过string对象的索引最好也是string :: size_type类型)
String对象中字符的处理
以下为字符函数库中常用的函数:
函数名称 | 返回值 |
isalnum() | 如果参数是字母数字,即字母或数字,该函数返回true |
isalpha() | 如果参数是字母,该函数返回真 |
isblank() | 如果参数是空格或水平制表符,该函数返回true |
iscntrl() | 如果参数是控制字符,该函数返回true |
isdigit() | 如果参数是数字(0~9),该函数返回true |
isgraph() | 如果参数是除空格之外的打印字符,该函数返回true |
islower() | 如果参数是小写字母,该函数返回true |
isprint() | 如果参数是打印字符(包括空格),该函数返回true |
ispunct() | 如果参数是标点符号,该函数返回true |
isspace() |
如果参数是标准空白字符,如空格、进纸、换行符、回车 、水平制表符或者垂直制表符,该函数返回true |
isupper() | 如果参数是大写字母,该函数返回true |
isxdigit() | 如果参数是十六进制的数字,即0~9、a~f、A~F,该函数返回true |
tolower() | 如果参数是大写字符,则返回其小写,否则返回该参数 |
toupper() | 如果参数是小写字母,则返回其大写,否则返回该参数 |
string s1; while(getline(cin,s1)){ for(string::size_type i=0;i<s1.size();i++){ if(ispunct(s1[i])){//如果存在标点符号 s1[i] = ' '; } } cout<<s1<<endl; }
综合实例:
/** String 类的使用 2013 10 26 */ #include <iostream> #include <string> //使用string的时候要加上这个 using namespace std; int main(){ //初始化string对象,一共有4种方式 cout << "This is the instalization of string :" << endl; //方式一 string s1; //初始化s1为空的串 cout << "S1 = " << s1 << endl; //方式二 string s2(s1); //将s2初始化成s1的副本 cout << "S2 is the copy of s1 :" << s2 << endl; //方式三 string s3(""); //将s3初始化成字符串的副本 cout << "S3 = " << s3 << endl; //方式四 string s4(10, 'c'); //将s4初始化成10个c的副本 cout << "s4 is : " << s4 << endl; system("echo off"); system("color 2"); //用getline来读取整行文本 string line; cout << "Please input a line words: And this is using the function getline()" << endl; getline(cin, line); cout << line << endl; //string中对象的操作 string stest("http://blog.csdn.net/hu1020935219"); //声明stest对象 //s.size() cout << "The size of " << stest << " is: " << stest.size() << endl; //s.empty() if(stest.empty()){ //判断string对象是否为空的一种方法:s.empty() cout << "stest is empty(using s.empty())" << endl; }else{ cout << "stest is not empty(using s.empty())" << endl; } //使用s.size()和0进行比较尽心判断 if(stest.size() == 0){ //判断string对象是否为空的一种方法:s.size() cout << "stest is empty(using s.size())" << endl; }else{ cout << "stest is not empty(using s.size())" << endl; } //从string 对象中获得字符,采用下标操作 string s5("http://blog.csdn.net/hu1020935219"); cout << "逐个输出string对象中字符:(using 下标操作)" <<endl ; for (string::size_type ix = 0; ix != s5.size(); ++ix){ //注意一下++ix和ix++的区别 cout << s5[ix] << ' '; } cout << endl ; //string中单个字符的操作:比如判断单个字符是否为大写字母 string s6("HuZhenjie"); string::size_type upcount = 0; for (string::size_type index = 0; index != s6.size(); ++index){ //++i 和 i++ if (isupper(s6[index])){ ++upcount; } } cout << "There are " << upcount << " UpperLetters in " << s6 << endl; //将大写字母转换成小写字母 for (string::size_type ind = 0; ind != s6.size(); ++ind){ //++i 和 i++ s6[ind] = tolower(s6[ind]); //进行大小写转换,将大写字母转换成小写字母 } cout << "the String is: HuZhenjie, and Then use tolower() we get:"; cout << s6 << endl; return 0; }