C++回顾(十九)—— 容器string
创始人
2024-06-01 07:21:27

19.1 string概述

  • 1、string是STL的字符串类型,通常用来表示字符串。而在使用string之前,字符串通常是 用char * 表示的。string 与char * 都可以用来表示字符串,那么二者有什么区别呢。

  • 2、string和 char * 的比较
    (1)string是一个类, char*是一个指向字符的指针。
    (2)string封装了char * ,管理这个字符串,是一个char * 型的容器。
    (3)string不用考虑内存释放和越界。
    (4)string管理char * 所分配的内存。每一次string的复制,取值都由string类负责维护,不用担心复制越界和取值越界等。

  • 3、string提供了一系列的字符串操作函数(这个等下会详讲)
    查找find,拷贝copy,删除erase,替换replace,插入insert

19.2 string构造

(1)默认构造函数:

string();    //构造一个空的字符串string s1。

(2)拷贝构造函数:

string(const string &str);  //构造一个与str一样的string。如string s1(s2)。

(3)带参数的构造函数

string(const char *s);    //  用字符串s初始化
string(int n,char c);    //  用n个字符c初始化

例子:
在这里插入图片描述
完整示例代码:

#include 
#include using namespace std;int main()
{string s1;                 //无参构造函数string s2("helloworld");   //有参构造函数string s3(10, 'a');   string s4(s2);            //拷贝构造函数cout << s1 << endl;   //重载了输出运算符 << cout << s2 << endl;cout << s3 << endl;cout << s4 << endl;cin >> s1;   //重载了输入运算符 >> cout << s1 << endl;s1 += "helloworld";  // 重载了 += 运算符if (s1 == s2)	// 重载了 == 运算符{}s1 = s1 + s2;   // 重载了 = 运算符return 0;
}

运行结果:
在这里插入图片描述

19.3 string使用

(1)string的存取字符操作:[ ] 或者 s.at( )

  • string类的字符操作:
const char &operator[] (int n) const; // 常函数(不改变成员变量)
const char &at(int n) const;  // 常函数(不改变成员变量)
char &operator[] (int n);
char &at(int n);
  • operator[]和at()均返回当前字符串中第n个字符,但二者是有区别的。
    主要区别在于at()在越界时会抛出异常,[ ]在刚好越界时会返回(char)0,再继续越界时,编译器直接出错。如果你的程序希望可以通过try,catch捕获异常,建议采用at()。
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述
    完整示例代码:
#include 
#include using namespace std;int main()
{string s("helloworld");cout << s[1] << endl;        //重载了下标运算符s[1] = 'x';cout << s << endl;cout << s.at(1) << endl;     //通过成员函数来访问下标为 1 的元素//cout << s[20000] << endl;    //越界访问程序异常结束try{cout << s.at(10) << endl;      //使用成员函数at(),越界访问会抛出异常}catch (exception &e){cout << e.what() << endl;}return 0;
}

运行结果:
在这里插入图片描述

(2)从string取得字符串首地址的操作:s.c_str( )

  • const char *c_str() const; //返回一个以’\0’结尾的字符串的首地址
    在这里插入图片描述
    完整示例代码:
#include 
#include 
#include using namespace std;int main()
{char buf[32] = {0};string s("helloworld");strcpy(buf, s.c_str());     //c_str()返回字符串的首地址cout << buf << endl;return 0;
}

运行结果:
在这里插入图片描述

(3)把string拷贝的操作:s.copy( )

把字符串s从第0给下标开始,拷贝5个字符到buf中去。
在这里插入图片描述在这里插入图片描述
完整示例代码:

#include 
#include using namespace std;int main()
{char buf[32] = {0};string s("helloworld");s.copy(buf, 5);               //第二个参数表示拷贝5个字节,第三个参数是默认参数,拷贝的位置,默认是0cout << buf << endl;memset(buf, 0, sizeof(buf));s.copy(buf, 4, 5); // 拷贝四个字节,从下标为5的字符开始拷贝cout << buf << endl;return 0;
}

运行结果:
在这里插入图片描述

(4)string的长度:s.length() 与 s.empty()

完整示例代码:

#include using namespace std;int main()
{string s("helloworld");cout << s.length() << endl;if (s.empty()){cout << "长度为空" << endl;}else{cout << "长度不为空" << endl;}return 0;
}

运行结果:
在这里插入图片描述

(5)string的赋值:重载的 = 运算符 与 s.assign()

示例代码:

#include using namespace std;int main()
{string s1("helloworld");s1 = "hello";        //重载了=运算符cout << s1 << endl;const char *s = "this is test";s1.assign(s);  // 把s这个字符串赋值给s1cout << s1 << endl;s1.assign(s, 7); //  把s这个字符串的前7个字符赋值给s1cout << s1 << endl;s1.assign(5, 'a');     //把5个a赋值给s1cout << s1 << endl;string s2("hey boy");s1.assign(s2);         //把对象s2赋值给s1cout << s1 << endl;s1.assign(s2, 4, 3); // 把s2从第四个字符开始往后3个字符,赋值给字符串s1cout << s1 << endl;return 0;
}

运行结果:

在这里插入图片描述

(6)string字符串连接:重载 += 运算符 与 s.append()

示例代码:

#include using namespace std;int main()
{string s1("helloworld");s1 += "1234";    //重载了 += 运算符cout << s1 << endl;string s2("abcdefg");s1 += s2;cout << s1 << endl;const char *s = "haha";s1.append(s);cout << s1 << endl;s1.append(s, 2); // 把 s 前两个字符加在s1后面cout << s1 << endl;s1.append(s2);cout << s1 << endl;s1.append(s2, 4, 2);  // 把 s 第四个字符开始往后两个字符加在s1后面cout << s1 << endl;s1.append(10, 'x'); // 把10个x字符追加在字符串cout << s1 << endl;return 0;
}

运行结果:
在这里插入图片描述

(7)string的比较:s.compare()

示例代码:

#include using namespace std;int main()
{string s1("helloworld");string s2("helloboy");const char *s = "hellogirl";if (s1.compare(s2) > 0){cout << s1 << " > " << s2 << endl;}if (s1.compare(s) > 0){cout << s1 << " > " << s << endl;}return 0;
}

运行结果:
在这里插入图片描述

(8)string的子串:s.substr()

示例代码:

#include using namespace std;int main()
{string s("helloworld");cout << s.substr() << endl; // 返回完整的字符串scout << s.substr(5, 5) << endl; // 从下标五开始,往后五个字符return 0;
}

运行结果:

在这里插入图片描述

(9)string的查找和替换:s.find() 与 s.replace()

  1. 查找
    int find(char c,int pos=0) const; //从pos开始查找字符c在当前字符串的位置

    int find(const char *s, int pos=0) const; //从pos开始查找字符串s在当前字符串的位置

    int find(const string &s, int pos=0) const; //从pos开始查找字符串s在当前字符串中的位置

    注意:find函数如果查找不到,就返回-1

    int rfind(char c, int pos=npos) const; //从pos开始从后向前查找字符c在当前字符串中的位置
    int rfind(const char *s, int pos=npos) const; int rfind(const string &s, int pos=npos) const;
    //rfind是反向查找的意思,如果查找不到, 返回-1

  2. 替换
    string &replace(int pos, int n, const char *s);//删除从pos开始的n个字符,然后在pos处插入串s
    string &replace(int pos, int n, const string &s); //删除从pos开始的n个字符,然后在pos处插入串s
    void swap(string &s2); //交换当前字符串与s2的值

完整示例代码:

#include 
#include using namespace std;int main()
{int p;string s1("helloworld");string s2("world");p = s1.find('o');cout << p << endl; // 4p = s1.find('x');    //不存在返回-1cout << p << endl; // -1p = s1.find('o', 5);cout << p << endl; // 6p = s1.find("ll");cout << p << endl; // 2p = s1.find(s2);cout << p << endl; // 5p = s1.rfind('o');    //反向查找(但是返回的位置还行从左边0开始)cout << p << endl; // 6s1.replace(5, 5, "xxx");cout << s1 << endl; // helloxxxxxs1.replace(5, 3, s2);cout << s1 << endl; // helloworldstring s3("helloworldhelloworldhelloworldhelloworld");p = s3.find("world");while (p != -1){s3.replace(p, strlen("world"), "x");p = s3.find("world", p + strlen("x"));}cout << s3 << endl;return 0;
}

运行结果:
在这里插入图片描述

(10)String的区间删除和插入:s.insert() 与 s.erase()

完整示例代码:

#include using namespace std;int main()
{string s1("helloworld");string s2("12345");s1.insert(0, "this is ");// 从下标为0处,插入this iscout << s1 << endl; s1.insert(10, s2); // 从下标为10处,插入s2cout << s1 << endl; s1.insert(0, 5, 'x'); // 从下标为0处,插入5个xcout << s1 << endl;s1.erase(0, 20);cout << s1 << endl; // 删除下标0到20return 0;
}

运行结果:
在这里插入图片描述

相关内容

热门资讯

美国因格陵兰岛问题对欧洲8国加... 当地时间17日,美国总统特朗普表示,将从2月1日起对丹麦、挪威、瑞典、法国、德国、英国、荷兰和芬兰出...
仓山区人工智能公共服务平台上线 17日上午,由仓山区产业投资集团牵头建设的仓山区人工智能公共服务平台上线。平台以“让中小企业低门槛用...
福清旅游集散中心投用 记者17日从福清市获悉,福清旅游集散中心在福清高铁西站正式投用。作为福清市旅游公共服务核心枢纽,该中...
两起无人机“黑飞”案被查处 警... 福清市公安局巡特警反恐大队持续整治无人机“黑飞”行为,查处两起无人机“黑飞”案件。去年9月,某航空科...
喜迎2026年福州市两会|福州... 科学教育筑基 化工人才扩容 外贸人才提质福州下好人才培养“先手棋”福州作为全国智慧教育示范区和“数字...