OSDN Git Service

Update container-adapter.md
authorakakw1 <44433637+akakw1@users.noreply.github.com>
Wed, 21 Aug 2019 08:55:31 +0000 (16:55 +0800)
committerGitHub <noreply@github.com>
Wed, 21 Aug 2019 08:55:31 +0000 (16:55 +0800)
docs/lang/csl/container-adapter.md

index beb2d48..91d43cd 100644 (file)
@@ -2,6 +2,46 @@ author: Xeonacid
 
 ## stack
 
+STL 栈 (`std::stack`) 是一种先进后出 (`FILO`) 的容器,仅支持查询或删除最后一个加入的元素(栈顶元素),不支持随机访问,且为了保证数据的严格有序性,不支持迭代器
+
+### 头文件和命名空间
+
+```cpp
+#include <stack>
+using std::stack
+```
+
+### 栈的定义
+
+```cpp
+stack<TypeName> s; // 使用默认底层容器 deque,数据类型为 TypeName
+stack<TypeName, Container> s; // 使用 Container 作为底层容器
+
+stack<TypeName> s2(s1); // 以 s1 为模板定义一个栈 s2
+```
+
+### 成员函数
+#### 以下所有函数均为常数复杂度
+- `top()` 访问栈顶元素(如果栈为空,此处会出错)
+- `push(x)` 向栈中插入元素 x
+- `pop()` 删除栈顶元素
+- `size()` 查询容器中的元素数量
+- `empty()` 询问容器是否为空
+
+### 简单示例
+
+```cpp
+stack<int> s1;
+s1.push(2);
+s1.push(1);
+stack<int> s2(s1);
+s1.pop();
+std::cout << s1.size() << " " << s2.size() << endl; // 1 2
+std::cout << s1.top() << " " << s2.top() << endl; // 2 1
+s1.pop();
+std::cout << s1.empty() << " " << s2.empty() << endl; // 1 0
+```
+
 ## queue
 
 ## priority_queue