常用 STL 操作
常用 STL 操作
可参考 cpp_container , 非常详细
====================
顺序容器 Sequence containers
- array
- vector
- deque
- forward_list
- list
关联容器 Associative containers
- set
- map
- multiset
- multimap
无序关联容器 Unordered associative containers
- unordered_set
- unordered_map
- unordered_multiset
- unordered_multimap
容器适配器 Container adaptors
- stack
- queue
- priority_queue
1. array
#include<array> #include<iostream> #include<string> #include<string_view> #include<algorithm> using namespace std; int main() { // 初始化,以及取元素操作 std::array<int, 5> a = {1, 2, 3, 4, 5}; // template<class T, size_t N> struct array; cout << a.at(1) << endl; cout << a[2] << endl; cout << a.front() << endl; cout << a.back() << endl; cout << a.data()[1] << endl; cout << "===========" << endl; // 迭代器 for (auto& element: a) { cout << element << "\t"; } cout << endl; for (std::array<int, 5>::iterator ite = a.begin(); ite != a.end(); ite++){ cout << *ite << "\n"; } auto print = [](const int i) {std::cout << i << ' ';}; // lambda 表达式 std::for_each(a.cbegin(), a.cend(), print); // for_each 算法 cout << "\n===========" << endl; // 容量检查 cout << a.empty() << endl; cout << a.size() << endl; cout << a.max_size() << endl; return 0; }