STL 基本操作
一、 字符串。
1. 使用前需包含头文件<string>,即使用前应有语句:#include<string>。
2. 该类重载了以下操作符:
a. =:赋值号。完成字符串的复制。
b. +、+=:完成字符串的连接。
c. <、>、<=、>=:字符串的比较。
d. <<、>>:字符串的输入输出。
e. []:下标访问控制符。
3. 成员函数。
a. int length();返回字符串所含字符个数。
b. int nd_rst_of(char ch,int start=0):从 start 开始搜索字符 ch,若找到,返回其位置,
否则返回-1.
c. int nd_last_of(char ch);查找 ch 最后出现的位置,成功返回位置,否则返回-1.
d. int nd(string&s,int start=0):从 start 位置开始搜做字符串 s,成功则返回其首次出现
位置,否则返回-1.
e. string subsrt(int start=0,int count=-1);从 start 位置开始复制 count 个字符,作为字串
返回。若字符数少于 count 或 count=-1 则复制到结尾。
f. void insert(int start,string&s):在 start 位置插入字符串 s。
g. void erase(int start=0,int count=-1):从 start 开始删除 count 个字符,当 count 为-1 时删
到结尾。
h. char * c_str();返回字符串的 c 语言格式的首地址。
二、 向量容器。
1. 使用前需包含头文件<vector>,即使用语句:#include<vector>。
2. Vector 特点:类似数组,可以用下标随机访问,不同之处在于它可以改变大小,在
中间插入删除效率较低。
3. 构造函数:
a. Vector();创建一个空向量,这是默认的构造函数。
b. Vector(int n,const T&value=T());创建具有 n 个元素的向量,每个元素初始值为
value。注意:类型 T 必须有默认的构造函数。
c. Vector(T*rst,T*last);用地址范围[rst,last]初始化向量。
4. 其它操作:
a. T& back();返回向量尾部数据项的引用。
b. const T& back() const;back 的常量版本。
c. bool empty():若向量为空则返回 true,否则返回 false。
d. void push_back(const T&value);在向量尾部增加一个数值 value。
e. void pop_back();删除向量尾部数据项。
f. void resize(int n,const T& ll=T());改变向量大小,若果增加大小,则用 ll 填充到
向量尾部。
g. int size();返回向量中元素个数。
三、 表容器。
1. 使用前需包含头文件<list>,即使用语句:#include<list>.