【Cpp::STL】标准模板库

慈云数据 6个月前 (05-29) 技术支持 26 0

标题:【Cpp::STL】标准模板库_ string详解

@水墨不写bug



目录

前言:创作感悟即更新方向预告

(一)string简介

 (二)容器介绍

(1)成员变量

(2)成员函数 

 i,构造函数string()

ii,析构函数~string()

iii,赋值重载

iv,迭代器

a,begin()

b,end()

c,rbegin()

d,rend()

e,cbegin()

f,cend()

g,crbegin()

h,crend()

v,容量和长度

a,size()

b,lenth()

c,max_size()

d,resize()

e,capacity()

f,reserve()

g,clear()

h,empty()

i,shrink_to_fit()

vi,元素访问

a,operator[]

b,at()

c,back()

d,front()

vii,修改方式

a,operator+=()

b,append()

c,push_back()

d,assign()

        将一个新值赋给字符串,替换其当前内容。

e,insert()

f,erase()

g,replace()

h,swap()

i,pop_back()

viii,串操作

a,c_str()

b,data()

c,get_allocator()

d,copy()

e,find()

f,rfind()

g,find_first_of()

h,find_last_of()

i,find_first_not_of()

j,find_last_not_of()

k,substr()

l,compare()

ix,成员常量

npos


前言:创作感悟即更新方向预告

        从C语言到C++,这一路走来,我们遇到了不少困难,并一路克服了他们。

        在刚接触C语言时,我们甚至觉得想要写对一句C语言代码都十分困难,但是随着练习的增多,我们在熟悉了基本语法之后,又遇到了C语言阶段的第一个拦路虎:《指针》。在指针阶段我们从不同层次,不同方面认识了:一级指针,二级指针;指针数组,数组指针;函数指针,函数指针数组等。

        在克服指针之后,我们就来到了比较平缓的区域,那时,我们主要学习了一些C的其他的小的语法。填填补补,讨论了一些学习者在学习C语言时其他的重要的问题。

        后来,我们再一次攀登了数据结构的高峰,数据结构是现实的一些问题的抽象出来的数据在计算机中的存储方式,数据结构是为了解决现实的一些问题,为了方便表示现实中的一些问题的数据而设计的。

        在数据结构阶段,我们熟悉了顺序表,单链表,栈,队列,带头双向循环链表,堆,二叉树等基本数据结构,并用C语言在底层实现了它们,这为我们深入学习高阶数据结构(后期会更新)奠定了坚实的基础!数据结构虽然困难,在能力区之外才能提升自己。

        之后,我们又从底层了解了常用排序:冒泡排序,插入排序,选择排序,堆排序,快速排序,希尔排序,归并排序等。通过C语言实现这些排序算法,我们对这些排序的适用情形,底层原理都有了深入的认识。

        接下来在进入C++学习之后,我们发现C++更加智能了,一些基本的数据结构不需要我们手搓实现了,但是我们对基本数据结构的认识仍然会对我们的学习提供莫大的帮助!STL提供的数据结构的设计原理和理由我们都可以一一道出。

        在此阶段,我们将进入C++的标准模板库的学习,深入剖析STL的原理。接下来,我会逐一表达我对STL的常用容器的理解:string(姑且算是容器)、vector、list、map、set、unordered_map等等。

希望接下来的内容对你的学习有所帮助!


(一)string简介

        string类是存储字符串类型的类。

        标准string类为这种对象提供了类似于标准字节容器的接口,但增加了专门用于操作单字节字符的字符串的功能。

The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.

        string类是basic_string类模板的实例化,basic_string类模板使用char(即bytes)作为其字符类型,其默认的char_traits和allocator类型(有关模板的更多信息,请参阅basic_string)。

The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).

        请注意,这个类处理字节时与使用的编码无关:如果用于处理多字节或变长字符序列(如UTF-8),该类的所有成员(如length或size)以及它的迭代器仍将以字节(而不是实际编码的字符)为单位进行操作。

Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).

 (二)容器介绍

(1)成员变量

member typedefinition
value_typechar
traits_typechar_traits
allocator_typeallocator
referencechar&
const_referenceconst char&
pointerchar*
const_pointerconst char*
iteratora random access iterator to char (convertible to const_iterator)
const_iteratora random access iterator to const char
reverse_iteratorreverse_iterator
const_reverse_iteratorreverse_iterator
difference_typeptrdiff_t
size_typesize_t

(待更新) 

(2)成员函数 


 i,构造函数string()

default (1)
string();

默认构造,创建一个空串,这个空串的长度是0;

#include
#include
using namespace std;
int main()
{
	string s1;
	cout
微信扫一扫加客服

微信扫一扫加客服

点击启动AI问答
Draggable Icon