C++之String容器


我们在C语言中定义一个字符串时,一般都是用一个字符类型的指针char *来创建一个字符串,或者创建一个字符数组,而在C++中提供了一个容器类,他的底层封装好了指针,并且还提供了很多方便的函数供我们来调用

string构造函数

既然是一个类,那么肯定里面是有构造函数的,而构造函数有这几种

无参的构造函数

string s1;

参数为字符串的构造函数

string s2("aaa");

拷贝构造函数

string s3(s2);

参数是字符的构造函数

string s4(10,'a');

string赋值

在C语言中,char *型虽然可以指向任意字符串,但是会涉及到开辟空间的问题,一不小心可能会有野指针或者段错误,在C++中字符串赋值就封装好了可以安全操作的函数。

1.等号赋值,这里的等号其实是容器中重载过的等号,原型是string & operator =();
2.类内函数assign
#include <iostream>
#include <string>
using namespace std;

void test(){
        string s1;
        s1="hello world";
        cout<<s1<<endl;
        string s2=s1;
        cout<<s2<<endl;
        string s3;
        s3='h';
        cout<<s3<<endl;
        string s4;
        s4.assign("你好");
        cout<<s4<<endl;
        string s5;
        s5.assign(s4);
        cout<<s5<<endl;
        string s6;
        s6.assign("nihao",3);
        cout<<s6<<endl;
        string s7;
        s7.assign(10,'b');
        cout<<s7<<endl;
}
int main(){
        test();
        return 0;
}

请添加图片描述

string字符串拼接

C语言中提供了一个字符串拼接的函数strcat(),在C++中也提供了

1.重载+=运算符:原型是string & operator+=();
2.类内函数append
#include <iostream>
#include <string>
using namespace std;

void test(){
        string s1="A";
        s1+="B";
        cout<<s1<<endl;
        s1+='C';
        cout<<s1<<endl;
        string s2="D";
        s1+=s2;
        cout<<s1<<endl;
        s1.append("E");
        cout<<s1<<endl;
        s1.append("FGHIJ",1);
        cout<<s1<<endl;
        string s3="G";
        s1.append(s3);
        cout<<s1<<endl;
        s1.append("abcdefg",2,3);
        cout<<s1<<endl;
}
int main(){
        test();
        return 0;
}

请添加图片描述
注意这里拼接的都是字符串所以不能接char类型的,赋值是可以直接赋值char类型

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最后一次出现位置

如果没找到则返回-1

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

字符串比较

和C语言中的strcmp()几乎一样

int  compare(const string & s) const
int compare(const char *s) const

返回值有三个,1,-1,0
当两个字符串相等返回0,s1>s2则返回1,s1<s2则返回-1

字符串存取

因为字符串底层就是字符数组,所以也可以通过下标来读或存

1.char & operator[](int)
2.内部的at函数
#include <iostream>
#include <string>
using namespace std;
void test(){
        string s1="abcdefg";
        for(int i=0;i<s1.size();i++){
                cout<<s1[i];
        }
        cout<<endl;
        for(int i=0;i<s1.size();i++){
                cout<<s1.at(i);
        }
}
int main(){
        test();
        return 0;
}

请添加图片描述

string插入和删除

string& insert(int pos,const char* s);
//插入字符串
string& insert(int pos, const string& str);
//插入字符串
string& insert(int pos, int n, char c);
//在指定位置插入n个字符c
string& erase(int pos, int n = npos);
//删除从Pos开始的n个字符

string获取子串

C语言中有strstr()获取子串

string substr(int pos,int n) const
返回从pos开始的n个字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值