• 第16章-cpp string类和标准模板库



    本章内容包括:

    • 标准C++string类
    • 模板auto_ptr、unique_ptr和shared_ptr
    • 标准模板库(STL)
    • 容器类
    • 迭代器
    • 函数对象(functor)
    • STL算法
    • 模板initializer_list

    一、string类

    构造字符串
    使用构造函数时都进行了简化,即隐藏了这样一个事实:string实际上是模板具体化basic_string的一个typedef,同时省略了与内存管理相关的参数(这将在本章后面和附录F中讨论)。size_t是一个依赖于实现的整型,是在头文件string中定义的。string类将静态成员常量string::npos定义为字符串的最大长度。

    关于npos
    static const size_t npos = -1;
    npos是一个静态成员常量值,是size_t类型的元素的最大可能值。
    当该值用作字符串成员函数中参数的值时,表示“直到字符串结束”。
    作为返回值,它通常用于指示不匹配。
    该常量的定义值为-1,因为size_t是无符号整数类型,所以它是该类型的最大可能表示值。

    // str1.cpp -- introducing the string class
    #include 
    #include 
    // using string constructors
    int main() {
        using namespace std;
        string one("Lottery Winner!");     // ctor #1  (注:ctor是constructor的缩写)
        cout << one << endl;               // overloaded <<
        string two(20, '$');               // ctor #2
        cout << two << endl;
        string three(one);                 // ctor #3
        cout << three << endl;
        one += " Oops!";                   // overloaded +=
        cout << one << endl;
        two = "Sorry! That was ";
        three[0] = 'P';                    // overloaded []
        string four;                       // ctor #4
        four = two + three;                // overloaded +, =
        cout << four << endl;
        char alls[] = "All's well that ends well";
        string five(alls, 20);              // ctor #5
        cout << five << "!\n";
        string six(alls+6, alls + 10);     // ctor #6
        cout << six  << ", ";
        string seven(&five[6], &five[10]); // ctor #6 again
        cout << seven << "...\n";
        string eight(four, 7, 16);         // ctor #7
        cout << eight << " in motion!" << endl;
        // std::cin.get();
        return 0; 
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    Lottery Winner!
    $$$$$$$$$$$$$$$$$$$$
    Lottery Winner!
    Lottery Winner! Oops!
    Sorry! That was Pottery Winner!
    All's well that ends!
    well, well...
    That was Pottery in motion!
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    C++11新增的构造函数
    构造函数string(string&& str)类似于复制构造函数,导致新创建的string为str的副本。但与复制构造函数不同的是,它不保证将str视为const。这种构造函数被称为移动构造函数(move constructor)。在有些情况下,编译器可使用它而不是复制构造函数,以优化性能。第18章的“移动语义和右值引用”一节将讨论这个主题。

    构造函数string(initializer_list il)让您能够将列表初始化语法用于string类。也就是说,它使得下面这样的声明是合法的:

    string piano_man = {'L', 'i', 's', 'z', 't'};
    string comp_lang {'L', 'i', 's', 'p'};
    
    • 1
    • 2

    string类输入
    未完待续。。。。。。

  • 相关阅读:
    用Dockerfile引入镜像报错
    学成在线第四天
    MySQL数据库基础知识回顾
    基于SSM实现的艺术品鉴定管理系统+App
    基于SqlSugar的开发框架循序渐进介绍(13)-- 基于ElementPlus的上传组件进行封装,便于项目使用
    联合主键NULL值过多,ON DUPLICATE KEY UPDATE不更新的问题
    SpringBoot项目--如何不停服更新应用?
    训练神经网络解决二分类问题的原理
    【C++入门篇】深入理解函数重载
    更简单的掩码图像建模框架SimMIM介绍和PyTorch代码实现
  • 原文地址:https://blog.csdn.net/itzyjr/article/details/126427545