3-2 C++ string类型

目录
3.2.1 定义和初始化
- 直接初始化(direct initialization):直接调用对应的构造函数,对于string而言,不带“=”即为直接初始化,主要就是列表初始化(间2-2-1),可用圆括号或者大括号
- 拷贝初始化(copy initialization):间接调用构造函数(一般是拷贝构造函数),对于string而言,带“=”即为拷贝初始化
 
- 具体辨析
3.2.2 对string的操作
(一)重要操作一览

(二)读写操作
cin/cout
- 读入cin>>s;:遇到空格就停止读入
- 输出cout<<s;
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s1;
    cin>>s1;  //读入s1
    cout<<s1;  //输出s1 
    return 0;
}
当输入Hello World时,s1只读取到Hello,World在缓冲区没被读取,所以值输出Hello
getline : 可读取整行
基本语法
getline(cin, s);
- 参数:输入流,字符串s
- 副作用:
- 读取输入流的一行,包括/n
- 将内容去除/n后存入字符串s
 
- 读取输入流的一行,包括
- 返回值:输入流对象
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s;
    getline(cin, s); //读入整行
    cout<<s<<endl;  //输出
    return 0;
}
输入Hello World时,s全部读取,输出Hello World
注意
- 如果一行的首字符就是/n,那么字符串s为空
- 因为getline不读取换行符,所以在输出时一般加上<<endl进行换行并清空缓冲区
(三)获取大小 .size()
string s; unsigned int = s.size();
- 
是string类的一个成员函数 
- 
返回字符串s的字符个数,类型是unsigned int【其实是string::size_type,但string::size_type底层就是unsigned int】 
- 
避免unsigned int 与 int 混用 string s = "hello"; if(s.size() < -1) //该条件判断式永真,因为-1会转换为一个很大的unsigned int cout<<"in if statement"<<endl; int length = s.size(); if(length < -1) //判断式为假,因为length是int类型 cout<<"in if statement"<<endl;
(四)比较、赋值与增加
比较: ==
- 参数:string 或 字符串字面量(string literals)
- 副作用:按字典序比较
- 返回值:返回比较结果,bool类型
#include<iostream>
#include<string>
using namespace std;
int main(){
    string s1 = "h";
    string s2 = "h";
    char s3[] = "h";
    bool i = (s1 == s2);  //比较s1与s2
    bool j = (s1 == s3);  //比较s1和s3
    cout<<"i = "<<i<<endl;  //输出 “i = 1”
    cout<<"j = "<<j<<endl;  //输出 “j = 1“
    return 0;
}
- 注
- string == string:比较内容
- string == string literals:比较内容
- string == char:不可比较
- string literals == string literals:比较指针地址【比较内容要用strcmp()函数】
 
赋值: =
- 
参数:string对象 或 字符串字面量 
- 
副作用:把string对象或字符串字面量的值赋值到另一个string中 
- 
返回值:string对象 #include<iostream> #include<string> using namespace std; int main(){ string s1 = "h"; //把字符串字面量的值赋给s1 string s2; s2 = s1; //把s1的值赋给s2 cout<<s1<<endl; //输出 "h" return 0; }
增添: +
- 
参数:string对象 或 字符串字面量 或 字符字面量, 但是 +的其中一个操作数必须是string对象
- 
副作用:生成一个新string对象,把 +左边操作数的内容赋给生成的string对象,再把右边操作数的内容赋给string对象
- 
返回值:string对象的引用 #include<iostream> #include<string> using namespace std; int main(){ string s1 = "Hello"; string s2 = "World"; string s3 = s1 + s2; //链接s1,s2,把值赋给s3 cout<<s3<<endl; //输出 “HelloWorld” return 0; }
- 
辨析:+`的其中一个操作数必须是string对象 string s1 = "a"; string s2 = s1 + "bc" + '\n'; //合法: 第一个+返回一个string对象,继续和'\n'相加【类似cin/cout链式法则】 string s3 = "bc" + '\n' + s1; //不合法:第一个+两侧的操作数没有string对象。
3.2.3 对string中字符的操作
(一)处理string的所有字符
遍历的三种方式
- 迭代器(Iterator):暂略
- 范围for
- 下标与一般for
范围for
- 
语法 for (declaration : expression) //expression:可遍历序列; declaration:expression基本元素的类型 statement; //对每个元素执行的操作
- 
举例:输出string的每个字符 #include<iostream> #include<string> using namespace std; int main(){ string s = "hello world"; for(auto c : s) cout<<c<<endl; return 0; }
cctype头文件
继承自c语言的ctype.h,但在c++最好写作cctype

- isnum(c)- isalpha(c)- isupper(c)&&- toupper(c)
- islower(c)&&- tolower(c)
 
- isdigit(c)
 
- ispunct(c)
- isspace(c)
几个应用实例
- 
统计输入的字符串 s中 标点符号的个数#include<iostream> #include<string> #include<cctype> using namespace std; int main(){ string s = "Hello,,world!!!"; int num = 0; for(char &c : s){ if(ispunct(c)) num++; } //输出 "the number of punctuation is 5" cout<<"the number of punctuation is "<<num<<endl; return 0; }
- 
将输入的字符串 s中的 所有小写字母转为大写【注意使用引用!】#include<iostream> #include<string> #include<cctype> using namespace std; int main(){ string s = "Hello,,world!!!"; for(char &c : s){ if(islower(c)) c = toupper(c); } //输出 “HELLO,,WORLD!!!” cout<<s<<endl; return 0; }
(二)处理string中的部分字符:下标(subscripts)
相关说明
- 
可以使用下标直接访问并修改对应的字符值【随机访问并且可以直接修改】 s[inxdex] = 'n'
- 
下标 index的类型是 string::size_type,即unsigned int,但也可以被自动类型转换为int
- 
index范围: 0<= index < s.size() 
用下标遍历
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(decltype(s.size()) index = 0; index < s.size(); ++index)  //index类型也可以直接写为int
        cout<<s[index];
    return 0;
}
引用实例:输出string s的第一个单词
以空格符号为分隔
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(int index = 0; index != s.size() && !isspace(s[index]);
        ++index)
        cout<<s[index];
    //输出 “Hello”
    return 0;
}
也可写作
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(char c : s){
        if(isspace(c))  break;
        cout<<c;
    }
    //最终输出 "Hello"
    return 0;
}
 
                    
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号