【C++】string 类 ( 上)

标准库中的string类

注意:

  1. string是表示字符串的字符串类

  2. 该类的接口与常规容器的接口基本相同,再添加了一些专门用来操作string的常规操作。 比特就业课

  3. string在底层实际是:basic_string模板类的别名,typedef basic_string string;

  4. 不能操作多字节或者变长字符的序列。 在使用string类时,必须包含#include头文件(#include<string>)以及using namespace std;

a. string类对象的常见构造

代码举例1

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1; // 相当于类对象的实例化
}

代码举例2

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1("hello world"); // 调用构造函数
	cout << t1 << endl;
	string t2 = "hello world"; //隐式类型转换(构造函数 + 拷贝构造 + 优化 -> 构造函数)
	cout << t2 << endl;
}

代码举例3

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1(10, 'a');  // 拷贝 10 个 a
	cout <<  t1 << endl;
}

运行结果:

代码举例4

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1("hello");
	string t2(t1); // 拷贝构造
	cout << t2 << endl;
}

b. string类对象的容量操作

  • size (返回字符串有效字符长度,没有 '\0 ')

代码举例1

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << t1.size() << endl;
}

运行结果:

  • capacity (返回字符串的总空间大小)

代码举例2

复制代码
#include <iostream>
#include<string>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << t1.capacity() << endl;
}

运行结果:

分析:

string 类里面的成员变量有两个可以存储空间,一个是数组,另一个是动态开辟的空间,当数组空间不足时,才会用动态开辟

  • reserve(扩大字符串容量,字符有效长度不变:即 size 不变)

代码举例3

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.reserve(100);
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
}

运行结果:

分析:

有些编译器在分配空间的时候,可能会对于开辟所需的空间再给大一点

  • resize (将有效字符的个数该成n个,多出的空间用字符c填充)

代码举例4

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello";
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(100);
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(10); //可以缩小有效长度,但总容量不会随意变动
	cout << "有效长度:" << t1.size() << " 总容量:" << t1.capacity() << endl;
	t1.resize(20, '*'); //对于的空间可以初始化任意字符
	cout << t1 << endl;
}

运行结果:

c. string类对象的访问及遍历操作

  • operator[] (返回pos位置的字符,和 C 语言的用法一样,const string类对象调用)
  • begin + end (begin获取一个字符的迭代器 + end获取最后一个字符下一个位置的迭代器)

代码举例1

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	string::iterator it = t1.begin();
	// it 相当于拿到 首元素的地址了
	while (it != t1.end())
	{
		cout << *it << endl;
		it++;
	}
}

运行结果:

分析:

  • rbegin + rend(rbegin获取最后一个字符的迭代器 + rend获取第一个字符前一个位置的迭代器)

代码举例2

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	string::reverse_iterator rit = t1.rbegin();
	// it 相当于拿到 首元素的地址了
	while (rit != t1.rend())
	{
		cout << *rit << endl;
		rit++;
	}
}

运行结果:

分析:

  • 范围for

代码举例3

复制代码
#include <iostream>
using namespace std;
int main()
{
	string t1 = "hello bit";
	for (auto i : t1)
	{
		cout << i;
	}
	cout << endl;
	for (int i = 0; i < t1.size(); i++)
	{
		cout << t1[i];
	}
}

运行结果:

相关推荐
handler016 小时前
从源码到二进制:深度拆解 Linux 下 C 程序的编译与链接全流程
linux·c语言·开发语言·c++·笔记·学习
t***5447 小时前
如何在Dev-C++中使用Clang编译器
开发语言·c++
Qbw20047 小时前
【Linux】进程地址空间
linux·c++
6Hzlia8 小时前
【Hot 100 刷题计划】 LeetCode 739. 每日温度 | C++ 逆序单调栈
c++·算法·leetcode
良木生香8 小时前
【C++初阶】:STL——String从入门到应用完全指南(1)
c语言·开发语言·数据结构·c++·算法
Kurisu_红莉栖9 小时前
c++复习——const,static字
c++
czxyvX9 小时前
1-Qt概述
c++·qt
齐鲁大虾10 小时前
新人编程语言选择指南
javascript·c++·python·c#
CoderMeijun10 小时前
C++ 多线程进阶:Lambda、条件变量与死锁
c++·多线程·条件变量·lambda·死锁·生产者消费者
unicrom_深圳市由你创科技11 小时前
上位机开发常用的语言 / 框架有哪些?
c++·python·c#