Standard Template Library
一、头文件
使用C++的推荐写法:
#include <cstdio>
#include <cstring> // string.h
#include <string>
#include <algorithm> // 常用算法头文件
二、string
C++提供了string类型,可以直接对字符串进行操作
包含在头文件string中
#include <string> // 区分cstring
三、sort
- 头文件 #include <algorithm>
- 默认排序为升序
- 可以对任意对象进行排序,对于内置类型直接排序,自己定义的类型需要定义<运算符
- 排序对象可以是普通数组,也可以是vector
- 函数原型:
void sort(begin_add, end_add, cmp)
// 表述形式:
sort(a, a + len); // char/int/double a[]
sort(s.begin(), s.end()) // string s
sort(v.begin(), v.end()) // vector<typename> v
- 自定义排序方式:
// char/int/double a[]
bool cmp(int x, int y) {
if(x < y) return true; // 升序
else return false;
}
// class Test
class Test {
public:
int i;
int j;
};
bool cmp(Text x, Text y) {
if(x.i < y.i) return true;
else if(x.i > y.i) return false; // 按照i升序排序
else {
if(x.j > y.j) return true;
else return flase; // 在i相同时按照j降序排序
}
}
四、sstream
- 头文件#include <sstream>中包括stringstream类,进行流的输入输出操作
- 常用操作:读一行,输出输入未知数量整数的和
#include <sstream>
#include <iostream>
using namespace std;
int main(void) {
string line;
while(getline(cin, line) { // 从cin按行读入直至结束
stringstream ss(line); // 将line的数据重新定义为流ss
int x, sum = 0;
while(ss >> x) sum += x;
cout << sum << "\n";
}
}
- 流的清空:
stringstream ss;
ss.clear();
五、iterator
- 对于不支持下标访问的容器可以用iterator进行迭代,事实上推荐用迭代器进行容器的遍历
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
vector<string> :: iterator it; // it可以读写vector<string>元素
vector<string> v;
// 正序迭代
for(vector<string> :: iteraotr it = v.begin(); it != v.end(); it++) {
/* 其他操作 */
}
// 逆序迭代
for(vector<string> :: reverse_iterator it = v.rbegin(); it != v.rend(); it++) {
/* 其他操作 */
}
}
- 迭代器的失效
// erase造成的迭代器失效
// 正确写法
std::vector<int> Vec;
std::vector<int>::iterator itVec;
for( itVec = Vec.begin(); itVec != Vec.end(); )
{
if( WillDelete( *itVec) )
itVec = Vec.erase( itVec); // erase返回下一个迭代器的值
else itList++;
}
- 迭代器访问容器元素:
// list vector set
list<int>::iterator it; *it;
// map
map<int, int>::iterator it; it->first; it->second;
-
erase函数释放迭代器:不可重复释放,对于释放过一次的迭代器再次释放会炸掉
-
访问迭代器的下一个元素用next()函数
六、vector
- 包含在头文件 #include <vector> 中
- 定义
vector<储存的类型> 容器名;
vector<string> v;
- 成员函数:
vector<int> v;
v.front() //返回首个元素
v.back() //返回尾部元素
v.push_back() //在末尾添加一个函数
v.pop_back() //弹出最后一个元素
v.size() //返回返回容器中元素个数
v.empty() //判断是否为空
v.clear() //清空容器
v.insert() //在指定位置插入元素
v.erase() //在指定位置删除元素
// insert函数详细用法
// v = [1,2,3,4] v1 = [5,6]
v.insert(v.begin() + 1, 5); // 1 5 2 3 4
v.insert(v.begin() + 1, {5,6,7}); // 1 5 6 7 2 3 4
v.insert(v.begin() + 1, 2, 5); // 1 5 5 2 3 4
v.insert(v.begin() + 1, v1.begin(), v1.end()) // 1 5 6 2 3 4
七、set
- 包含在头文件 #include <set>中
- 自动有序(升序),不含重复元素的容器
- 定义
set<储存的类型> 容器名;
set<int> s;
- 成员函数:
set<int> s;
s.insert() // 插入元素并自动排序去重,复杂度O(logN)
s.erase() // 删除元素
s.count() // 返回值为value的元素的个数,1或0
s.find() // 返回迭代器,复杂度为O(logN)
s.size() //返回返回容器中元素个数
s.empty() //判断是否为空
s.clear() //清空容器
s.lower_bound() // 返回指向大于(或等于)某值的第一个元素的迭代器
八、map
- 包含在头文件 #include <map>中
- map中元素是自动按key升序排序(从小到大)
- map是key到value的映射,重载了[]运算符,类似于数组的高级版
- 定义
map<key, value> m;
- 成员函数:
typedef map<string, int> MyMap;
MyMap mymap;
// 操作对象为key
mymap.insert()
mymap.erase()
mymap.count()
mymap.find()
mymap.size()
mymap.empty()
mymap.clear()
- insert详细用法:
#include <iostream>
#include <algorithm>
#include <string>
#include <map>
using namespace std;
typedef map<string, int> monthMap;
void print(monthMap monthmap) {
for(map<string, int>::iterator it = monthmap.begin(); it != monthmap.end(); it++) {
cout << it->first << " " << it->second << endl; // iterator访问map元素方法
}
}
int main(void) {
// 直接赋值法
monthMap monthmap1;
monthmap1["aaa"] = 1;
monthmap1["bbb"] = 2;
monthmap1["ccc"] = 3;
print(monthmap1);
// insert插入
// insert pair data
monthMap monthmap2;
monthmap2.insert(pair<string, int>("aaa", 11));
monthmap2.insert(pair<string, int>("bbb", 22));
monthmap2.insert(pair<string, int>("ccc", 33));
print(monthmap2);
// insert value_type data
monthMap monthmap3;
monthmap3.insert(map<string, int>::value_type("aaa", 111));
monthmap3.insert(monthMap::value_type("bbb", 222));
monthmap3.insert(monthMap::value_type("ccc", 333));
print(monthmap3);
}
- find
// 查找的对象是it->first,可以理解为原本的数组下标
monthMap monthmap;
monthmap["aaa"] = 1;
monthmap["bbb"] = 2;
monthmap["ccc"] = 3;
cout << monthmap.count("aaa") << endl; // return 1|0
monthMap::iterator it = monthmap.find("aaa"); // return iterator