【STL】string详解

目录

一、string介绍

​二、string的常用接口及使用

1.初始化及定义

string的创建

拼接

拷贝构造

2.string的遍历

1.operator[ ]重载

2.通过迭代器遍历

3.使用范围for遍历

4.使用at

3.string的容量相关接口

4.增删查改

1.push_back

2.append

3.+=

4.insert()

5.erase

6.replace

7.find系列

8.补充:getline


一、string介绍

STL(Standard Template Library)是C++标准库的一部分,提供模板类和函数,用于实现常用的数据结构和算法。

string是C++中用于管理字符串的一个类,属于STL的一部分。

二、string的常用接口及使用

1.初始化及定义

string的创建

//头文件
#include<string>

//初始化及定义
    string s1;//默认空字符串
    string s2("hello");
    string s3="helloworld"; //单参数的构造函数支持隐式类型转化
    string s4("1234",0,3); //结果为123,从0位置开始长度为3
    string s5("1234",2); //结果为12,长度为2
    string s6(5,'2'); //结果为22222,5个2连接而成的字符串

拼接

int main()
{
    string s1="hello";
    string s2="world";

    //拼接

    string s3=s1+s2; //s3结果为"helloworld"
    string s4=s3+s3; 
    string s5=s3+"hello"; //s5结果为"helloworldhello"
}

拷贝构造

​
int main()
{
    string s1("hello");
    string s2(s1);//完全拷贝
    
    //部份拷贝
    string s3(s1,1,5)//从s1下标为一的位置开始取5个字符,5>s1的字符数目,从起始位置处取s1整个字符串
    string s4(s1,1);//第三个缺省参数默认-1(size_t),从位置1处取完
    
    //迭代器区间拷贝构造初始化
     string s6(s1.begin(),s1.end());//end获得的是'\0'的位置
     
}

​

2.string的遍历

1.operator[ ]重载

for(int i=0;i<s1.size();i++)
cout<<s1[i]<<endl;

cout<<s1[i]++<<
//可读可写

2.通过迭代器遍历

//正向迭代器
int main()
{
    sting s1("hello");
    string::iterator::it=s1.begin();//iterator是迭代器,封装在string内部,it是迭代器类型
    while(it!=s1.end())//end是'\0'
    {
        cout<<*it;
        it++;    
    }
}
//反向迭代器
int main()
{
    string s1("helloworld");
    string::reserve_iterator rit=s1.begin();
    while(rit!=s1.rend())
    {
        cout<<*rit;
        rit++;    
    }
}

3.使用范围for遍历

for(auto ch:s1)
{
    cout<<ch<<' ';
}

4.使用at

for(size_t i=0;i<s1.size();i++)
{
    cout<<s1.at(i)<<' ';
}
//at和operator[]无本质区别

3.string的容量相关接口

size()

计算字符串中有效字符个数,不包含'\0';

capacity()

计算当前字符串容量

同一个字符串,不同编译器下给定容量可能不同(不包括‘\0')

resize(n)

1.n<=size()

尾删元素,size减小到n

2.n>=size&&n<capacity

尾插,size增加到n,默认插入'\0'

3.n>capacity

相当于插入元素,size增加到n,capacity增加

reserve(n)

用来提前申请空间

1.n>capacity,将空间扩容到n

2.n<=capacity,不变

empty()

判空,bool类型,空返回1;不为空返回0;

clear()

清空字符串,size为0,capacity不变

4.增删查改

1.push_back

用于尾插单个字符

s.push_back('a');//在末尾插入'a'

2.append

追加,尾插,可追加整个字符串,也可追加字符串一部分

string s1="helleo";
s1.append('w');
s1.append("world");
s1.append(s2);
s1.append(s2,0,3);//从s2的0位置开始往后3个字符追加到s1中
s1.append(2,'w');//追加两个'w';

3.+=

string s1("hello");
string s2("world");

s1+=s2;
//s1为helloworld;

string s3="hello";
s3+="world";
//s3为helloworld;

4.insert()

在指定位置插入字符或字符串

//插入字符
string s1("hello world");
s1.insert(0,2,'*') //从s1的下标为0位置开始插入两个*;
s2.insert(s2.begin(),'w'); //从s2的起始位置开始插入w;
s3.insert(s3.begin(),2,'w'); //从s3的起始位置开始插入两个w;
//插入字符串
s1.insert(2,"ssss"); //从s1下标为2位置插入ssss
s1.insert(2,s3,2,6); //取s3字符串下标从2开始的6个字符插入s1下标为2的位置

5.erase

用于删除指定位置起的一个或若干个字符

s1.erase(2,5);//删除下标从2开始的5个字符
s1.erase(7);//删除下标从7开始的剩余字符
s1.erase();//删除整个字符串

6.replace

用新的字符或者字符串代替原字符串的部分字符

string s1("hello world");
s1.replace(2,5,"xxxxxxxxxxx");//从s1下标为2开始5个字符换成"xxxxxxxxxxxxxx";
//结果为 : hexxxxxxxxxxxxxorld;
s1.replace(++s1.begin(),--s1.end(),"xxxxxxxx")
//结果为:hxxxxxxxxd;
s1.replace(2,5,  "xxxxxxxxxx",3,4)从下标为2开始的5个字符换成xxxxxxxxxxx下标为3开始的4个字符
s1.replace(2,5,3,'*');从下标为2开始的5个字符换成3个'*';

7.find系列

方法作用示例
find查找子字符串或字符s.find("hello")
rfind逆向查找子字符串或字符s.rfind("hello")
find_first_of查找字符集合中的任意一个字符s.find_first_of("aeiou")
find_last_of查找字符集合中的任意一个字符(最后一次)s.find_last_of("aeiou")
find_first_not_of查找不在字符集合中的字符s.find_first_not_of(" \t")
find_last_not_of查找不在字符集合中的字符(最后一次)s.find_last_not_of(" \t")

如果找到目标字符或子字符串

  • 返回 首次(find)/最后一次(rfind)出现的位置(下标),从 0 开始计数。

  • string s = "hello world";
    cout << s.find('o');  // 输出 4(第一个 'o' 的下标)
    cout << s.rfind('o'); // 输出 7(最后一个 'o' 的下标)

如果未找到目标字符或子字符串:

  • 返回 string::nposnpos 是 std::string 的静态常量,表示 "not found")。

  • npos 的值通常是 size_t 类型的最大值(如 4294967295 或 18446744073709551615,取决于系统位数)。

8.补充:getline

cin无法读取到空格和换行,因此cin输入默认输入内容是以字符或者换行分隔的

getline可以直接读取一整行

string s1;
getline(cin,s1);

//想要读取回车
getline(cin,s1,'\r');(第三个参数指定输入内容之间的分隔符);CTRL+z结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值