standard template libaray标准模板库
一、标准容器
1.顺序容器
vector 向量
deque 双端队列
list 链表
2.容器适配器
stack 栈
queue 队列
priority_queue 优先级队列
3.关联容器
无序关联容器 链式哈希表O(1)
unordered_set 无序的单重集合
unordered_map 无序的单重映射表
unordered_multiset 无序的多重集合
unordered_multimap 无序的多重映射表
有序关联容器 红黑树 O(log2n)
set 单重集合
multiset 多重集合
map 单重映射表
multimap 多重映射表
二、近容器
数组,string,bitset 位容器(哈夫曼)
三、迭代器
iterator和const_iterator
reverse_iterator和const_reverse_iterator
四、函数对象
greater,less(类似C的函数指针)
五、泛型算法
sort,find,find_if,binary_search,for_each
vector
#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<int> vec;
for (int i = 0; i < 20; ++i)
{
vec.push_back(rand() % 100 + 1);
}
int size = vec.size();
for (int i = 0; i < size; ++i)
{
cout << vec[i] << " ";
}
cout << endl;
vector<int>::iterator it2 = vec.begin();
for (; it2 != vec.end();)
{
if (*it2 %2 == 0)
{
it2 = vec.erase(it2);
}
else
{
++it2;
}
}
for (it2= vec.begin(); it2 != vec.end();++it2)
{
}if (*it2 % 2 != 0)
{
it2 = vec.insert(it2, *it2 - 1);
++it2;
}
vector<int>::iterator it1 = vec.begin();
for (;it1 != vec.end(); ++it1)
{
cout << *it1 << " ";
}
cout << endl;
return 0;
}
deque和list
#include <iostream>
#include <vector>
using namespace std;
int main()
{
}
顺序容器的对比
#include<iostream>
using namespace std;
int main()
{
return 0;
}
容器适配器
#include <iostream>
#include <deque>
using namespace std;
template<typename T,typename Container=deque<T>>
class Stack
{
public:
void push(const T& val)
{
con.pushback(val);
}
private:
Container con;
};
int main()
{
return 0;
}
无序关联容器
#include <iostream>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
using namespace std;
int main()
{
unordered_set<int> set1;
for (int i = 0; i < 20; ++i)
{
set1.insert(rand() % 20 + 1);
}
unordered_set<int>::iterator it1 = set1.begin();
for (; it1 != set1.end(); ++it1)
{
cout << *it1 << endl;
}
unordered_map<int, double> map1;
map1.insert(make_pair(1000, 10.01));
map1.insert({ 1001,10.02 });
map1[200] = 12.06;
for (pair<int, double> p : map1)
{
if (p.second > 1)
{
cout << "key" << endl;
}
}
return 0;
}
有序关联容器
#include<iostream>
#include <set>
#include <map>
using namespace std;
class Student
{
public:
Student(int id, string name)
:_id(id), name(name)
{
}
bool operator< (const Student stu)const
{
return _id < stu._id;
}
private:
int _id;
string name;
};
int main()
{
set<int> set1;
for (int i = 0; i < 20; ++i)
{
set1.insert(rand() % 20 + 1);
}
for (int v : set1)
{
cout << v << endl;
}
}
迭代器
容器的迭代器
普通正向迭代器 iterator 可以修改指向元素的值
常量的正向迭代器 const_iterator 只能读而不能写
反向迭代器 reverse_iterator rbegin()最后一个元素反向迭代器表示 rend()返回首元素前驱位置的迭代器表示
常量反向迭代器 const_reverse_iterator
函数对象
#include <iostream>
using namespace std;
template<typename T>
bool greater(T a, T b)
{
return a > b;
}
template<typename T>
bool less(T a, T b)
{
return a < b;
}
template<typename T,typename Compare>
bool compare(T a, T b, Compare comp)
{
return comp(a, b);
}
class Sum
{
public:
int operator()(int a, int b)
{
return a + b;
}
private:
};
template<typename T>
class mygreater
{
public:
bool operator()(T a, T b)
{
return a > b;
}
};
template<typename T>
class myless
{
public:
bool operator()(T a, T b)
{
return a < b;
}
};
int main()
{
Sum sum;
int ret = sum(10, 15);
compare(10, 20, mygreater<int>());
compare(10, 20, myless<int>());
cout << ret << endl;
}
泛型算法
头文件 #include<algorithm>
sort/find/find_if/binary_serach/for_each
#include <iostream>
#include <vector>
#include <algorithm>
#include<functional>
using namespace std;
int main()
{
int arr[] = { 12,4,5,14,34,67,87,26,12,55,99 };
vector<int> vec(arr, arr + sizeof(arr) / sizeof(arr[0]));
sort(vec.begin(), vec.end());
for (auto v: vec)
{
cout << v << endl;
}
auto it1 = binary_search(vec.begin(), vec.end(), 21);
auto it2 = find_if(vec.begin(), vec.end(), bind1st(greater<int>(), 48));
vec.insert(it2, 48);
}