C++---string类常见接口

介绍

 string类详情===>>>https://cplusplus.com/reference/string/string/?kw=string

1. string是表示字符串的字符串类(感觉就像一个动态的字符数组)
2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。
3. string在底层实际是:basic_string模板类的别名,typedef basic_string<char, char_traits, allocator>string;
4. 不能操作多字节或者变长字符的序列。
在使用string类时,必须包含#include头文件以及using namespace std;

string类的常用接口说明

1 string类常用构造函数

1.1 string()

定义一个空字符串,直接使用就行。

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s1;
    return 0;
}

1.2 string(const char* s)

#include <iostream>
#include <string>
using namespace std;
int main()
{

    const char *ch="hello world";
    string s1("hello world");
    string s2(ch);
    
    return 0;
}

1.3 string(size_t n, char c)

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s(3, 'a'); //3个a字符
    cout << s;
    return 0;
}

1.4 string(const string&s)

#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s(3, 'a');
    string s1 = s;//定义变量时候的=就是拷贝构造
    cout << s;

    string s2;
    s2 = s;//定义完了之和的=是调用的运算符重载operator=()

    return 0;
}

2 string类对象的容量操作

2.1 size/length

	string s1("hello world");
	string s2("hello");
	cout << s1.size() << endl; //11
	cout << s2.length() << endl; //5

    //size和length本质没啥区别,只是谁先被创造出来的差别而已

2.2 capacity

string s1("hello world");
cout << s1.capacity() << endl; //15

//实际容量一般比capacity多一个,用于存储'\0'

capacity与size的区别:capacity是容量,表示总共最多能装多少个字符,而size是表示字符串的有效字符的数量,则capacity>=size。

在实际扩容中,为了提高效率,capacity的增长是按照一定比率增长的,在VS中,大概为1.5倍增长。

2.3 clear

string s1("hello world");
cout << s1  << endl; //hello world
s1.clear();
cout << s1  << endl; //空

2.4 reserve

string s;//创建一个s空字符串
s.reserve(100);//提前为 s 开辟100个空间
cout<<s.size()<<endl;//0
cout << s.capacity() <<endl;//111

 reserve只会开辟空间,并不会改变字符串有效字符内容。reserve开辟的空间要比string底层空间要大,如果传入参数小于string底层空间大小的话则为无效开辟。

2.5 resize

两种方式:

resize(size_t n) 与 resize(size_t n, char c)都是将字符串中有效字符个数改变到n个,不同的是当字
符个数增多时:resize(n)用0来填充多出的元素空间,resize(size_t n, char c)用字符c来填充多出的
元素空间。

注意:resize在改变元素个数时,如果是将元素个数增多,可能会改变底层容量的大
小,如果是将元素个数减少,底层空间总大小不变。

3 string类对象的访问和遍历

3.1 operator[] 

for (int i = 0; i < s3.size(); i++)
	{
		s3[i] += 2;
	}

[]的运算符重载,可以直接将string类对象当数组一样用下标访问。

3.2 begin+ end

//迭代器
	string::iterator it = s3.begin();
	while (it != s3.end())//写
	{
		*it -= 2;
		it++;
	}
	it = s3.begin();//置为起点
	while (it != s3.end())//读   end为数组末尾的下一个位置
	{
		cout << *it << " ";
		it++;
	}

begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器

3.3 rbegin + rend

string s1("hello world");
	string::reverse_iterator  rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		rit++;
	}
	cout << endl;

4 string类对象的修改操作

4.1 push_back

string s;
s.push_back('c');

在字符串后插入c

4.2 append

string s;
s.append("123");

在字符串后追加一个字符串

4.3 operator+=

string s;
s += '8';
s += "sda";
//s: 8sda

在字符串后追加字符串str

在string尾部追加字符时,s.push_back(c) / s.append(1, c) / s += 'c'三种的实现方式差不多,一般情况下string类的+=操作用的比较多,+=操作不仅可以连接单个字符,还可以连接字符串。

4.4 insert 

string s("hello world");
s.insert(s.begin(), 's');
//s: shello world

在下标为pos之前插入字符串。

4.5 erase

string s("xxxhello world");
s.erase(0,3);//前面的 0 是位置,后面的3是需要删除的字符个数
cout << s << endl; //hello world

从某个位置开始删除几个字符,如果字符不够删,则有多少删多少

4.6 c_str

string s1("sdjsd");
	const char* str = s1.c_str();
	while (*str)
	{
		cout << *str << " ";
		str++;
	}
	cout << endl;

返回首地址。

4.7 find + npos

string s("hello world");
int pos = s.find(' ');//找到‘ ’的位置
cout << s.substr(pos+1)<<endl;//world
int pos1 = s.find('l', pos);//从pos开始找l
cout << s.substr(pos1)<<endl;//ld

4.8 rfind

int pos2=s.rfind('l');
cout << s.substr(pos2)<<endl;
int pos3 = s.rfind('l', pos2-1);
cout << s.substr(pos3)<<endl;

rfind和find类似,find是从往右找,rfind是从右往左找。

4.9 substr

string s("hello world");
int pos = s.find(' ');//找到‘ ’的位置
cout << s.substr(pos+1)<<endl;//world
int pos1 = s.find('l', pos);//从pos开始找l
cout << s.substr(pos1)<<endl;//截取pos1开始到最后的字符段

在str中从pos位置开始,截取n个字符,然后将其返回。

5 string类非成员函数

5.1 operator+

尽量少用,因为传值返回,导致深拷贝效率低

5.2 operator>>

输入符重载

5.3 operator<<

输出运算符重载

5.4 getline

getline与cin类似但不相同,cin输入时遇到空格或者遇到换行符就会停止读入,而getline不会。

  • 16
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值