C++string的使用

· CSDN的uu们,大家好。这里是C++入门的第十六讲。
· 座右铭:前路坎坷,披荆斩棘,扶摇直上。
· 博客主页: @姬如祎
· 收录专栏:C++专题

目录

1.构造函数

1.1 string()

1.2 string(const char* s)

1.3 string(const char* s, size_t n)

1.4 string(size_t n, char c)

1.5 string (const string& str)

1.6 string (const string& str, size_t pos, size_t len = npos)

2. size_t size() const

3. size_t length() const

4. size_t max_size() const

5. resize

5.1 void resize (size_t n)

​编辑5.2 void resize (size_t n, char c)

6. size_t capacity() const

7: void reserve (size_t n = 0)

8. void clear()

9. bool empty() const

10. operator[]

11. char at(size_t pos) 

12. char& back()

13. char& front()

14. operator+=

15. string& append (const char* s)

​编辑

16. void push_back (char c)

17. insert()

17.1 string& insert (size_t pos, const char* s)

17.2 string& insert (size_t pos, size_t n, char c)

18. string& erase (size_t pos = 0, size_t len = npos)

19. void pop_back()

20. const char* c_str() const

21. find()

21.1 size_t find (const char* s, size_t pos = 0) const

21. 2 size_t find (char c, size_t pos = 0) const

22. rfind()

​编辑

22.1 size_t rfind (const char* s, size_t pos = npos) const

22.2 size_t rfind (char c, size_t pos = npos) const

23. string substr (size_t pos = 0, size_t len = npos) const


1.构造函数

1.1 string()

这是 string 的无参构造,在 string 的底层就是构造了一个空字符串:“”,即在下标为 0 的位置存储了一个 '\0' 。

#include<iostream>
#include<string>
using namespace std;

int main()
{
    string s;
    cout << s;
    return 0;
}

这是VS2022下看到的结果。VS对 string 的封装比较复杂,他一上来就开了很多空间,并且直接把开辟出来的空间全部初始化为了0。我们看到的现象会因编译器的不同有所差异。

1.2 string(const char* s)

这个构造函数表示可以使用常量字符串来初始化 string!或者使用字符数组来初始化,字符数组的数组名也是 char* 类型的嘛,char* 是可以用const char* 来接收的。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("csdn yyds!");
	cout << s << endl; //输出:csdn yyds!
    
    char arr[12] = { 'c', 's', 'd', 'n', ' ','y' ,'y', 'd', 's', '!'};
    string s1(arr);
    cout << s1 << endl; //输出:csdn yyds!

}

1.3 string(const char* s, size_t n)

 这个构造函数表示可以使用一个常量字符串 或者 字符数组的前 n 个字符来初始化 string。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("csdn yyds!", 4);
	cout << s << endl; //输出:csdn

	char arr[10] = { 'c', 's', 'd', 'n', ' ','y' ,'y', 'd', 's' };
	string s1(arr, 4);
	cout << s1 << endl; //输出:csdn
}

1.4 string(size_t n, char c)

这个构造函数表示可以用 n 个 字符 来构造一个string对象。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s(8, '8');
	cout << s << endl; //输出:88888888
}

1.5 string (const string& str)

这个构造函数表示可以使用另一个string对象来构造一个string对象(拷贝构造)。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("csdn yyds!");

	string s1(s);
	cout << s1 << endl; //输出:csdn yyds!
}

1.6 string (const string& str, size_t pos, size_t len = npos)

这个构造函数表示可以用另一个 string 对象 从 pos 位置开始截取 n 个字符来构造一个 string 对象。

我们看到 len 参数有个缺省值:npos。这个 npos 是定义在 string 类中一个public属性的静态常量,其值为 -1。但是因为 len 的类型为无符号的整形,因此 len 的实际大小是无符号整形的最大值。我们可以直接打印 npos :

#include<iostream>
#include<string>
#include<climits>
using namespace std;

int main()
{
	cout << (int)string::npos << endl; //int输出的npos
	cout << string::npos << endl; //size_t输出的npos
	cout << UINT32_MAX << endl; //32位机器下无符号整形的最大值

}

当我们的 pos + len 大于参数一字符串的长度时就是截取 pos 位置后的全部字符来构造string。不传 len ,len = npos 那可是相当大呢!所以不传 len 就是截取 pos 后面的全部字符来构造string。 

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("csdn yyds!");

	string s1(s, 5, 4); //从下标为 5 的位置(第一个y)向后截取 4 个字符来构造一个string
	cout << s1 << endl; //输出 yyds

	string s2(s, 5, 188); //len 传入很大
	cout << s2 << endl; //输出 yyds!

	string s3(s, 5); //不传len
	cout << s3 << endl; //输出 yyds!

}

2. size_t size() const

 这个函数用于返回一个 string 对象存储的有效字符的数量。为什么要有后面的那个const呢?我们知道这个const是修饰 *this 的,加上const 常对象与普通对象都可以调用啦!

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("I love scdn!");
	cout << s1.size() << endl; //输出:12

	const string s2("csdn yyds!");
	cout << s2.size() << endl; //输出:10
	
}

3. size_t length() const

 这个接口跟 size_t size() const 实现的效果是一毛一样的!那你就可能会问了,为什么设计者要搞出来两个功能相同的接口呢?那是因为 string 类的出现早于 STL 库,string 在很久以前是没有 size() 这个接口的。但是呢 STL出现以后,求容器的大小都是实现的 size(), 于是 string 就也加上了一个 size() 接口!

4. size_t max_size() const

这个函数返回 一个string 对象多能存储的最多的字符数量,每个编译器的结果不尽相同!此函数用得也不多!

5. resize

5.1 void resize (size_t n)

在 string 的底层维护了一个字符数组,一个表示有效字符个数的size,和字符数组实际的容量capacity。而 resize 函数就是用来改变有效字符的个数的函数。

情况1:当 n 小于原字符串的大小,底层实现很简单,直接改变size,在添加一个 '\0' 表示字符的结束即可! 因此会保留原字符串的数据。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("csdn yyds");
	s1.resize(4);
	cout << s1 << endl; //输出:csdn
	cout << s1.size() << endl; //输出:4
	
}

通过调试观察 string 的内存,也可以证明我的描述:

 

情况2: 当 n 大于原字符串的大小。会将原字符串的 size 扩展到相应的大小,但是在 VS2022 上,多余的空间他会全部初始化为 0 ,resize 之后的字符串的打印结果与原来并无不同。但是可以通过打印 size 来观察不同!

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("csdn yyds!");
	cout << s1.size() << endl; //resize之前输出:10
	s1.resize(20);
	cout << s1 << endl; //输出:csdn yyds!
	cout << s1.size() << endl; //输出:20
}

通过观察 string 内存,能够证实我的讲解: 

5.2 void resize (size_t n, char c)

 这是一个重载的版本,在改变字符串大小的同时增加了初始化的功能!这个初始化的功能仅在 n 的大小大于原字符串的大小时生效(也就是 5.1 情况二的时候生效。)

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("csdn yyds!");
	s1.resize(20, 'a');
	cout << s1 << endl; //输出: csdn yyds!aaaaaaaaaa
	cout << s1.size() << endl;//输出:20
}

6. size_t capacity() const

我们刚才提到 string 的底层维护了一个 capacity 用来表示字符数组的实际大小,这个函数就是用来返回这个 capacity 的。此函数用得也不多!

不同编译器对于 string 维护的数组开空间的规则不同,因此返回相同字符串的capacity,不同编译器得到的值也不一定相同。下面是VS2022的结果:

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s;
	cout << s.capacity() << endl; //输出:15
}

7: void reserve (size_t n = 0)

这个函数是用来改变 string 维护的字符数组的实际容量的。

8. void clear()

清空一个字符串的内容,使之变为空串!

9. bool empty() const

判断一个字符串是不是空串!

10. operator[]

operator[] 能够使得字符串支持下标访问,这个是平时用得非常多的!他提供了两个版本,一个是非const版本,一个是const版本。const 修饰的string对象,通过下标访问得到的字符不允许修改。而非const的string对象通过下标访问得到的字符可以修改。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello world");
	s1[5] = '#';
	cout << s1 << endl; //输出:hello#world
}

const修饰的string对象不可修改:

11. char at(size_t pos) 

此函数完全可以被operator[]代替哈,at函数返回的是下标为 pos 的字符。 同样也有const版本和非const版本。

12. char& back()

该函数返回字符串的最后一个字符,同样也有 const 和非const 两个版本。

const版本:const char& back() const

非const版本:char& back()

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello world");
	char& tail = s1.back(); //非const支持修改
	tail = 'C';
	cout << s1 << endl;//输出:hello worlC
}

13. char& front()

该函数返回字符串的第一个字符,同样有 const 和非 const 两个版本。

const char& front() const

char& front()

14. operator+=

string 重载了 += 运算符,支持一个字符串加等一个字符,一个字符串常量,一个string 对象。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s("csdn yyds!");
	s += 'a';
	cout << s << endl; //输出:csdn yyds!a

	s += "bbb";
	cout << s << endl; //输出:csdn yyds!abbb

	string tmp("ccc");
	s += tmp;
	cout << s << endl; //输出:csdn yyds!abbbccc
}

15. string& append (const char* s)

往一个string对象的末尾追加一个 常量字符串 或者 数组。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello ");
	s1.append("world");
	cout << s1 << endl; //输出:hello world
}

我们查阅 资料 发现 append 重载了很多版,是不是觉得string的设计有点冗余。个人感觉最好用的还是operator+=。 

16. void push_back (char c)

 往string后面追加一个字符,只能说非常鸡肋。不如operator+=。

17. insert()

我们可以看到 insert 重载了巨多版本,真令人头大呢!我们就讲讲两个具有代表性的。其他的都大同小异。

17.1 string& insert (size_t pos, const char* s)

 该函数用于在pos位置插入一个 常量字符串 或者 字符数组(字符数组一定要有 '\0' 哦)。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");
	s1.insert(5, " hi"); //在下标为5的位置插入 hi
	cout << s1 << endl; //输出:hello hi csdn
	
}

17.2 string& insert (size_t pos, size_t n, char c)

该函数用于在pos位置插入n个相同的字符。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");
	s1.insert(5, 5, '1'); //在下标为5的位置插入5个'1'
	cout << s1 << endl; //输出:hello11111 csdn
	
}

18. string& erase (size_t pos = 0, size_t len = npos)

这个函数可以将从pos位置开始,后面的len个字符全部删除,不传len,表示len很大,也就是移除pos后面的所有字符啦!

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");
	s1.erase(0, 6); //移除下标 0 后面的6个字符
	cout << s1 << endl; //输出:csdn
	
	string s2("hello csdn");
	s2.erase(1); //移除下标1后的所有字符
	cout << s2 << endl; //输出:h

}

19. void pop_back()

 这个函数简单,删除string的最后那个字符。

20. const char* c_str() const

这个函数可以返回 C风格的字符串。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");
	const char* s = s1.c_str();
	cout << s << endl; //输出:hello csdn
	return 0;
}

C风格的字符串:char类型的数组,每个下标对应一个字符,末尾有一个 '\0'。

21. find()

find 重载的版本也是比较多的呢!老规矩讲常用的!

21.1 size_t find (const char* s, size_t pos = 0) const

 这个函数用来在一个string中,从pos位置开始查找一个常量字符串。pos不传的话就是从头查找。如果查找到了,返回起始下标,如果没有查找到返回 npos 。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");
	//从下标为1的位置开始查找 csdn 这个常量字符串
	cout << s1.find("csdn", 1) << endl; //输出:6

	//从下标为7的位置开始查找 csdn 这个常量字符串
	cout << s1.find("csdn", 7) << endl; //输出:4294967295(这是无符号整形的最大值)
	cout << (int)s1.find("csdn", 7) << endl; //输出:-1

}

21. 2 size_t find (char c, size_t pos = 0) const

这个函数用来在一个string中,从pos位置开始查找字符 c。pos不传就是从头开始查找。一旦查找成功 (即从pos位置开始第一个字符 c ),返回查找成功的下标,否则返回 npos。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");

	//从下标为 1 的位置开始查找字符'l'
	cout << s1.find('l', 1) << endl; //输出:2

	//从下标为 4 的位置开始查找字符'l'
	cout << s1.find('l', 4) << endl; //输出:4294967295(这是无符号整形的最大值)
	cout << (int)s1.find('l', 4) << endl; //输出:-1
}

22. rfind()

我们可以看到参数的形式跟find完全一样!rfind只是从后往前找罢了!

22.1 size_t rfind (const char* s, size_t pos = npos) const

表示:在 string 对象中从pos位置往前,查找常量字符串 s 。pos不传就是从末尾向前查找。查找成功,返回起始位置的下标,查找失败,返回 npos。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");

	//从下标为 8 的位置往前,查找常量字符串 csdn
	cout << s1.rfind("csdn", 8) << endl; //输出:6

	//从下标为 5 的位置往前,查找常量字符串 csdn
	cout << s1.rfind("csdn", 5) << endl; //输出:4294967295(这是无符号整形的最大值)
	cout << (int)s1.rfind("csdn", 5); //输出:-1

}

22.2 size_t rfind (char c, size_t pos = npos) const

表示:从pos位置开始向前查找字符 c 。pos不传就是从末尾向前查找。一旦查找成功 (pos位置往前,第一个字符c的下标) 返回该下标,否则返回 npos。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");

	//从下标为 8 的位置开始向前查找字符'c'
	cout << s1.rfind('c', 8) << endl; //输出:6

	//从下标为 5 的位置开始向前查找字符'c'
	cout << s1.rfind('c', 5) << endl; //输出:4294967295(这是无符号整形的最大值)
	cout << (int)s1.rfind('c', 5) << endl; //输出:-1
}

23. string substr (size_t pos = 0, size_t len = npos) const

 这是用截取原string对象中的一部分连续字符,返回一个新的string对象。简言之:获取子串。

表示:从 pos 位置开始截取 len 个长度的字符来构造新的string。pos不传默认从头开始截取。len不传默认截取到末尾。

#include<iostream>
#include<string>
using namespace std;

int main()
{
	string s1("hello csdn");

	//不传len,默认截取到末尾,从 6 截取到末尾
	cout << s1.substr(6) << endl; //输出:csdn

	//从 0 开始截取 5 个字符
	cout << s1.substr(0, 5) << endl; //输出 hello
}

 总结:不需要死记硬背,用得多了自然就记得了!string的接口真的很多,记不起来查查文档就知道了!

string - C++ Reference (cplusplus.com)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姬如祎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值