前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >C++ string类学习笔记

C++ string类学习笔记

作者头像
LRainner
发布2020-07-16 22:59:00
4420
发布2020-07-16 22:59:00
举报
文章被收录于专栏:安全学习笔记安全学习笔记

string类的常用构造函数

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s1;  //空字符串
        string s2(10, '@');  //10个@
        string s3("Hello World");  //hello world        //等价于string s3 = "Hello World"
        string s4(s3);  //s4和s3一样
        string s5("hello world", 20);  //hello world        //将hello world存入s5,最多存储20个字节
        string s6("Hello world", 2, 20);  //llo world        //将Hello world的第2位作为字符串的开头,存到s6,最多存储20个字节}

使用string类时,必须包含头文件以及using namespace std

string类常用函数

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");        string c("GoodBye World");
        cout << s.length() << endl;  //11 返回字符串长度
        cout << s.size() << endl;  //11 和length()一样
        cout << s.capacity() << endl;  //15 返回s3的容量
        s.resize(10);  //Hello Worl 设置s的大小为10        //若大小大于当前字符串长度,\0来填充
        s.resize(20, '!');  //Hello Worl!!!!!!!!!!        //若大小大于当前字符串长度,!来填充
        s.reserve(10);  //Hello Worl        //设置s的容量为10,不会填充数据
        s.swap(c);  //GoodBye World 交换s和c
        s.push_back('!');  //GoodBye World!        //在s末尾添加 ! ,参数必须是字符形式
        s.append("HHH");  //GoodBye World!HHH        //在s末尾添加HHH,参数必须是字符串形式
        s.clear();  //清空s}

assign函数

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");
        s.assign("GoodBye");  //GoodBye        //清空s,并设置为GoodBye
        s.assign("GoodBye", 4);  //Good        //清空s,并设置为Good,保留两个字符
        s.assign("GoodBye", 1, 2);  //oo        //清空s,并设置为GoodBye的第1个字符开始,保留2个字符
        s.assign(5, '!');  //!!!!!        //清空s,并设置为!!!!!}

插入

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");
        s.insert(2, 2, '!');  //He!!llo World        //在s下标为2的位置插入2个!
        s.insert(2, "AAA");  //HeAAA!!llo World        //在s下标为2的位置插入AAA
        s.insert(2, "BCD", 1);  //HeBAAA!!llo World        //在s下标为2的位置插入 BCD 中的1个字符
        s.insert(2, "EFGH", 1, 2);  //HeFGBAAA!!llo World        //在s下标为2的位置插入 BCD 中从位置1开始的2个字符}

删除

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");
        s.erase(8);  //Hello Wo        //从下标为8的位置开始,之后全部删除
        s.erase(2, 1);  //Helo Wo        //从下标为2的位置开始,删除1个字符
        s.replace(2, 4, "ABCD");  //HeABCDo        //从下标为2的位置开始,替换4个字节为 ABCD
        s.clear();  //清空s
        cout<<s.empty()<<endl;  //1 判空,返回0/1}

反转

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");        reverse(s.begin(), s.end());  //dlroW olleH        //从s的开始到结束字符反转}

查找

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");

        cout << s.find("l") << endl;  //2        //查找l,返回第一次出现位置

        cout << s.find("World") << endl;  //6        //查找World,返回第一次出现位置

        cout << s.find('l', 3) << endl;  //3        //从下标为3的位置查找l,返回第一次出现位置

        cout << s.find("Worll", 2, 4) << endl;  //6        //从下标为2的位置查找 Worll 的前四个字符,返回第一次出现位置}

s.rfind():从尾部查找

s.find_first_of():查找是否含有字串中任何一个字符,并返回第一次出现位置

s.find_last_of():从尾部开始查找是否含有字串中任何一个字符,并返回第一次出现位置

拷贝

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s1("Hello World");        string s2;

        s2 = s1.substr(2);  //llo World        //提取从s1下标为2开始到末尾,并赋值给s2

        s2 = s1.substr(2, 3);//llo        //提取从s1下标为2开始,长度为3字节,并赋值给s2}

比较

代码语言:javascript
复制
#include<iostream>#include<string>using namespace std;int main(){        string s("Hello World");

        cout << s.compare("Hello World") << endl;  //0        //完全一样返回0,字符串大于s返回<0的值,字符串小于s返回>0的值

        cout << s.compare(0, 2, "HHH", 0, 3) << endl;  //1        //s的下标为0开始的两个字符 和 HHH的下标为0开始的3个字符比较}
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-01-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小白也编程 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档