咱们一块儿来学习
经常使用接口
string类常见构造:ide
函数名 | 功能说明 |
---|---|
string() | 构造空的string类对象,即空字符串 |
string(const char* s) | 用C-string来构造string类对象 |
string(size_t n, char c) | string类对象中包含n个字符c |
string(const string&s) | 拷贝构造函数 |
void Teststring() { string s1; // 构造空的string类对象s1 string s2("hello bit"); // 用C格式字符串构造string类对象s2 string s3(s2); // 拷贝构造s3 }
vector类常见构造函数:函数
函数名 | 功能说明 |
---|---|
vector() | 无参构造 |
vector(size_type n, const value_type& val = value_type()) | 构造并初始化n个val |
vector (const vector& x); | 拷贝构造 |
vector (InputIterator first, InputIterator last); | 使用迭代器进行初始化构造 |
std::vector<int> first; std::vector<int> second (4,100); std::vector<int> fourth (third); std::vector<int> third (second.begin(),second.end());
list类常见构造函数:学习
Column 1 | Column 2 |
---|---|
list() | 构造空的list |
list (size_type n, const value_type& val = value_type()) | 构造的list中包含n个值为val的元素 |
list (const list& x) | 拷贝构造函数 |
list (InputIterator first, InputIterator last) | 用[first, last)区间中的元素构造list |
std::list<int> l1; std::list<int> l2 (4,100); std::list<int> l4 (l3); std::list<int> l3 (l2.begin(), l2.end());
迭代器器操做
string,vector,list三个类迭代器操做如出一辙code
函数名 | 功能说明 |
---|---|
begin | 将迭代器返回到开头 |
end | 将迭代器返回到结尾 |
rbegin | 将反向迭代器返回到反向开始 |
rend | 将反向迭代器返回到反向结尾 |
空间容量操做
string,vector类的空间容量操做,length()函数是string类独有对象
函数名 | 功能说明 |
---|---|
size/length | 返回字符串有效字符长度 |
capacity | 返回空间总大小 |
max_size | 可供储存元素的个数的上线,一般是因为寻址空间决定的 |
empty | 检测字符串释放为空串,是返回true,不然返回false |
clear | 清空有效字符 |
reserve | 为字符串预留空间 |
resize | 将有效字符的个数该成n个,多出的空间用字符c填充 |
list类的空间容量操做:blog
函数名 | 功能说明 |
---|---|
size | 返回字符串有效字符长度 |
capacity | 返回空间总大小 |
max_size | 可供储存元素的个数的上线,一般是因为寻址空间决定的 |
修改元素操做
string,vector,list三个类共有的函数操做接口
函数名 | 功能说明 |
---|---|
push_back | 在末尾追加元素 |
pop_back | 删除末尾元素 |
insert | 插入元素 |
erase | 删除元素 |
swap | 交换元素 |
string独有操做ci
函数名 | 功能说明 |
---|---|
operator+= | 在字符串后追加字符串 |
list类独有操做rem
函数名 | 功能说明 |
---|---|
push_front | 头插 |
pop_front | 头删 |
vector,list共有函数字符串
函数名 | 功能说明 |
---|---|
clear | 清空内容 |
独有操做
每一个类型模板都有本身的一些独有操做函数
例如:string类的c_str等函数
而有些操做例如:
list类中unique(),remove(),remove_if(),sort(),reverse()
string类中find(),copy()等函数虽然在STL中都是独有的,可是在algorithm头文件(这个头文件的强大远出乎你的想象哦o(^@^)o)中都有涉及,意味着只要运用获得,reverse()也能够对string类的对象使用