• C++学习笔记(二十三)


    在完成对C语言的学习后,我最近开始了对C++和Java的学习,目前跟着视频学习了一些语法,也跟着敲了一些代码,有了一定的掌握程度。现在将跟着视频做的笔记进行整理。本篇博客是整理C++知识点的第二十三篇博客。

    本篇博客介绍了C++的string容器。

    本系列博客所有C++代码都在Visual Studio 2022环境下编译运行。程序为64位。

    目录

    string容器

    string容器基本概念

    string构造函数

    string赋值操作

    string拼接

    string查找和替换

    string比较

    string存取

    string插入和删除

    string子串获取


    string容器

    string容器基本概念

    string是C++风格的字符串,本质是一个类。使用string需要包含头文件string

    char *是一个指针。string是一个类,类内封装了char *,管理这个字符串。

    string内封装了很多成员方法。

    string管理char *分配的内存,不用担心复制越界和取值越界等问题,由类内进行负责。

    string构造函数

    string()创建一个空的字符串。

    string(const char * s)使用字符串进行初始化。

    string(const string& str)使用一个string对象初始化另一个string对象

    string(int n,char c)使用n个字符c进行初始化。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. const char* ch = "Hello C++ STL";
    7. string s1(ch);
    8. string s2(s1);
    9. string s3(10, 'h');
    10. cout << s1 << endl;
    11. cout << s2 << endl;
    12. cout << s3 << endl;
    13. return 0;
    14. }

    程序的输出是:

    Hello C++ STL
    Hello C++ STL
    hhhhhhhhhh

    string赋值操作

    string& operator=(const char* s)将char* 类型字符串赋值给当前的字符串。

    string& operator=(const string &s)把字符串s赋值给当前字符串。

    string& operator=(char c)将字符赋值给当前字符串。

    string& assign(const char* s)将字符串s赋值给当前字符串。

    string& assign(const char*s,int n)把字符串s的前n个字符赋给当前字符串。

    string& assign(const string &s)将字符串s赋值给当前字符串。

    string& assign(int n,char c)用n个字符c赋给当前字符串。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1;
    7. s1 = "Hello C++";
    8. cout << s1 << endl;
    9. string s2;
    10. s2 = s1;
    11. cout << s2 << endl;
    12. string s3;
    13. s3 = 'C';
    14. cout << s3 << endl;
    15. return 0;
    16. }

    程序的输出是:

    Hello C++
    Hello C++
    C

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1;
    7. s1.assign("Hello C++");
    8. string s2;
    9. s2.assign("Hello C++", 5);
    10. string s3;
    11. s3.assign(s2);
    12. string s4;
    13. s4.assign(10, 'C');
    14. cout << s1 << endl;
    15. cout << s2 << endl;
    16. cout << s3 << endl;
    17. cout << s4 << endl;
    18. return 0;
    19. }

    程序的输出是:

    Hello C++
    Hello
    Hello
    CCCCCCCCCC

    string拼接

    string& operator += (const char* str)重载+=运算符。

    string& operator += (const char c)重载+=运算符。

    string& operator += (const string& str)重载+=运算符。

    string& append(const char* s)将字符串s连接到当前字符串结尾。

    string& append(const char* s,int n)把字符串s的前n个字符连接到当前字符串结尾。

    string& append(const string &s)将s连接到当前字符串结尾。

    string& append(const string &s,int pos,int n)将字符串s从pos开始的n个字符连接到字符串结尾。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1("Hello");
    7. s1 += " C++";
    8. cout << s1 << endl;
    9. string s2("Iok");
    10. s2 += 'e';
    11. cout << s2 << endl;
    12. string s3("Sam");
    13. string s4(" FW");
    14. s3 += s4;
    15. cout << s3 << endl;
    16. return 0;
    17. }

    程序的输出是:

    Hello C++
    Ioke
    Sam FW

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1("Hello");
    7. s1.append(" C++");
    8. cout << s1 << endl;
    9. string s2("Hello");
    10. s2.append("C++ or Java", 3);
    11. cout << s2 << endl;
    12. string s3("Sam");
    13. string s4("FW");
    14. s3.append(s4);
    15. cout << s3 << endl;
    16. string s5("This is a test");
    17. string s6("There is end-term");
    18. s6.append(s5, 9, 5);
    19. cout << s6 << endl;
    20. return 0;
    21. }

    程序的输出是:

    Hello C++
    HelloC++
    SamFW
    There is end-term test

    string查找和替换

    int find(const string& str,int pos = 0) const;查找str第一次出现的位置,从pos开始查找。

    int find(const char* s,int pos = 0)const;查找s第一次出现的位置,从pos开始查找。

    int find(const char* s,int pos,int n)const;从pos位置查找s的前n个字符第一次出现的位置。

    int find(const char c,int pos = 0)const;查找字符c第一次出现的位置。

    int rfind(const string& str,int pos = npos)const;查找str最后一次出现的位置,从pos开始查找。

    int rfind(const char* s,int pos = npos)const;查找s最后一次出现的位置,从pos开始查找。

    int rfind(const char* s,int pos,int n)const;从pos开始查找s的前n个字符最后一次出现的位置。

    int rfind(const char c,int pos = 0)const;查找字符c最后一次出现的位置。

    string& replace(int pos,int n,const string& str)替换从pos开始的n个字符为字符串str。

    string& replace(int pos,int n,const char* s)替换从pos开始的n个字符为字符串s。

    位置指的是下标,从0开始。npos代表最后一个元素的位置。如果找不到,返回-1。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1 = "abcdefghijklmnopqrstuvwxyz";
    7. cout << s1.find("de") << endl;
    8. cout << s1.find("fr") << endl;
    9. cout << s1.find("dennis", 0, 2) << endl;
    10. cout << s1.find("dennis", 0, 3) << endl;
    11. cout << s1.find('i') << endl;
    12. cout << endl;
    13. string s2 = "JimanaJuanJoaquinJulianJavierJeanneJuliaJovaJoseJohnJoyceJulietteJerryJulioJosephine";
    14. cout << s2.find("Ju") << endl;
    15. cout << s2.rfind("Ju") << endl;
    16. cout << s2.find("Jo", 0, 2) << endl;
    17. cout << s2.rfind("Jo", 84, 2) << endl;
    18. cout << s2.find('i') << endl;
    19. cout << s2.rfind('i') << endl;
    20. return 0;
    21. }

    程序的输出是:

    3
    18446744073709551615
    3
    18446744073709551615
    8

    6
    70
    10
    75
    1
    81

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1 = "JimanaJuanJoaquinJulianJavierJeanneJuliaJovaJoseJohnJoyceJulietteJerryJulioJosephine";
    7. string s2 = "Jul";
    8. cout << s1.find(s2) << endl;
    9. cout << s1.rfind(s2) << endl;
    10. string s3 = "JK";
    11. cout << s1.find(s3) << endl;
    12. cout << s1.rfind(s3) << endl;
    13. string s4 = "abcedfghijklmnopqrstuvwxyz";
    14. s4.replace(20, 3, "20220717");
    15. cout << s4 << endl;
    16. s4.replace(9, 2, s3);
    17. cout << s4 << endl;
    18. }

    程序的输出是:

    17
    70
    18446744073709551615
    18446744073709551615
    abcedfghijklmnopqrst20220717xyz
    abcedfghiJKlmnopqrst20220717xyz

    string比较

    字符串比较按照ASCII码比较,相等返回0,大于返回1,小于返回-1。

    int compare(const string &s)const;与字符串s比较。

    int compare(const char* s)const;与字符串s比较。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1 = "hello";
    7. string s2("hello");
    8. cout << s1.compare(s2) << endl;
    9. cout << s1.compare("Hello") << endl;
    10. cout << s1.compare("hi") << endl;
    11. return 0;
    12. }

    程序的输出是:

    0
    1
    -1

    string存取

    char& operator[](int n)通过[]获取字符。

    char& at(int n)通过at方法获取字符。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s = "hello";
    7. int i;
    8. for (i = 0; i < s.size(); i += 1) {
    9. cout << s[i] << " ";
    10. }
    11. cout << endl;
    12. for (i = 0; i < s.size(); i += 1) {
    13. cout << s.at(i) << " ";
    14. }
    15. cout << endl;
    16. }

    程序的输出是:

    h e l l o
    h e l l o

    string插入和删除

    string& insert(int pos,const char* s)将s插入到pos的位置。

    string& insert(int pos,const string& str)将str插入到pos的位置。

    string& insert(int pos,int n,char c)将n个c插入到pos的位置。

    string& erase(int pos,int n = npos)删除从pos开始的n个字符。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s1 = "hello";
    7. s1.insert(1, "lll");
    8. cout << s1 << endl;
    9. s1.erase(1, 3);
    10. cout << s1 << endl;
    11. string s2 = "not";
    12. s1.insert(2,s2);
    13. cout << s1 << endl;
    14. s1.insert(4, 3, 'i');
    15. cout << s1 << endl;
    16. return 0;
    17. }

    程序的输出是:

    hlllello
    hello
    henotllo
    henoiiitllo
     

    string子串获取

    string substr(int pos = 0,int n = npos)const;返回从pos开始的n个字符组成的字符串。

    1. #include
    2. #include
    3. using namespace std;
    4. int main(void)
    5. {
    6. string s = "Keith";
    7. cout << s.substr(1, 2) << endl;
    8. cout << s.substr(3, 2) << endl;
    9. return 0;
    10. }

    程序的输出是:

    ei
    th

  • 相关阅读:
    SpringBoot测试及web环境模拟测试
    JSON基本语法
    SpringMVC学习笔记
    河北首家城商行传统核心业务国产化,TDSQL突破三“最”为秦皇岛银行保驾护航
    科普初步了解大模型
    Linux音频系统编程之芯片平台适配功放Codec Driver解读
    2023年前端面试题
    【iOS-UIImagePickerController访问相机和相册】
    Ansible的filter
    手把手教你部署nginx+php —— k8s从入门到高并发系列教程 (一)
  • 原文地址:https://blog.csdn.net/m0_71007572/article/details/126398304