3-2 Library string Type


image-20220115090332720

目录
  • 3.2.1 定义和初始化
  • 3.2.2 对string的操作
    • (一)重要操作一览
    • (二)读写操作
      • cin/cout
      • getline : 可读取整行
    • (三)获取大小 .size()
    • (四)比较、赋值与增加
      • 比较: ==
      • 赋值: =
      • 增添: +
  • 3.2.3 对string中字符的操作
    • (一)处理string的所有字符
      • 遍历的三种方式
      • 范围for
      • cctype头文件
      • 几个应用实例
    • (二)处理string中的部分字符:下标(subscripts)
      • 相关说明
      • 用下标遍历
      • 引用实例:输出string s的第一个单词

3.2.1 定义和初始化

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

3.2.2 对string的操作

(一)重要操作一览

image-20220112135550369

(二)读写操作

cin/cout

  • 读入cin>>s;遇到空格就停止读入
  • 输出cout<
#include
#include
using namespace std;
int main(){
    string s1;
    cin>>s1;  //读入s1
    cout<

当输入Hello World时,s1只读取到Hello,World在缓冲区没被读取,所以值输出Hello

getline : 可读取整行

基本语法

getline(cin, s);

  • 参数:输入流,字符串s
  • 副作用:
    • 读取输入流的一行,包括/n
    • 将内容去除/n存入字符串s
  • 返回值:输入流对象
#include
#include
using namespace std;
int main(){
    string s;
    getline(cin, s); //读入整行
    cout<

输入Hello World时,s全部读取,输出Hello World

注意

  • 如果一行的首字符就是/n,那么字符串s为空
  • 因为getline不读取换行符,所以在输出时一般加上<进行换行并清空缓冲区

(三)获取大小 .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"<

(四)比较、赋值与增加

比较: ==

  • 参数:string 或 字符串字面量(string literals)
  • 副作用:按字典序比较
  • 返回值:返回比较结果,bool类型
#include
#include
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 = "<赋值: =
  • 参数:string对象 或 字符串字面量

  • 副作用:把string对象或字符串字面量的值赋值到另一个string中

  • 返回值:string对象

    #include
    #include
    using namespace std;
    int main(){
        string s1 = "h";  //把字符串字面量的值赋给s1
        string s2;
        s2 = s1;  //把s1的值赋给s2
        cout<

增添: +

  • 参数:string对象 或 字符串字面量 或 字符字面量, 但是+的其中一个操作数必须是string对象

  • 副作用:生成一个新string对象,把+左边操作数的内容赋给生成的string对象,再把右边操作数的内容赋给string对象

  • 返回值:string对象的引用

    #include
    #include
    using namespace std;
    int main(){
        string s1 = "Hello";
        string s2 = "World";
        string s3 = s1 + s2;  //链接s1,s2,把值赋给s3
        cout<
  • 辨析:+`的其中一个操作数必须是string对象

    string s1 = "a";
    string s2 = s1 + "bc" + '\n'; //合法: 第一个+返回一个string对象,继续和'\n'相加【类似cin/cout链式法则】
    string s3 = "bc" + '\n' + s1; //不合法:第一个+两侧的操作数没有string对象。
    

3.2.3 对string中字符的操作

(一)处理string的所有字符

遍历的三种方式

  1. 迭代器(Iterator):暂略
  2. 范围for
  3. 下标与一般for

范围for

  • 语法

    for (declaration : expression) //expression:可遍历序列; declaration:expression基本元素的类型
        statement;  //对每个元素执行的操作
    
  • 举例:输出string的每个字符

    #include
    #include
    using namespace std;
    int main(){
        string s = "hello world";
        for(auto c : s)
            cout<

cctype头文件

继承自c语言的ctype.h,但在c++最好写作cctype

image-20220112175326993

  • isnum(c)
    • isalpha(c)
      • isupper(c) &&toupper(c)
      • islower(c)&&tolower(c)
    • isdigit(c)
  • ispunct(c)
  • isspace(c)

几个应用实例

  • 统计输入的字符串s中 标点符号的个数

    #include
    #include
    #include
    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 "<
  • 将输入的字符串s中的 所有小写字母转为大写【注意使用引用!

    #include
    #include
    #include
    using namespace std;
    int main(){
        string s = "Hello,,world!!!";
        for(char &c : s){
            if(islower(c))
                c = toupper(c);
        }
        //输出 “HELLO,,WORLD!!!”
        cout<

(二)处理string中的部分字符:下标(subscripts)

相关说明

  1. 可以使用下标直接访问并修改对应的字符值【随机访问并且可以直接修改s[inxdex] = 'n'

  2. 下标index的类型是 string::size_type,即unsigned int,但也可以被自动类型转换为int

  3. index范围: 0<= index < s.size()

用下标遍历

#include
#include
#include
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(decltype(s.size()) index = 0; index < s.size(); ++index)  //index类型也可以直接写为int
        cout<

引用实例:输出string s的第一个单词

以空格符号为分隔

#include
#include
#include
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(int index = 0; index != s.size() && !isspace(s[index]);
        ++index)
        cout<

也可写作

#include
#include
#include
using namespace std;
int main(){
    string s = "Hello world!!!";
    for(char c : s){
        if(isspace(c))  break;
        cout<

相关