Stack用法中文参考手册:https://zh.cppreference.com/w/cpp/container/stack
1. 头文件 < stack >
2. stack是什么
- std::stack是容器适配器,提供的是FILO(先进后出)数据结构,栈的功能
3. 成员函数
3.1 元素访问
元素访问 | 作用 |
---|---|
top | 访问栈顶元素 |
stack<string> st;
st.push("aaa");
st.push("bbb");
st.push("ccc");
cout<<st.top()<<endl; // 结果是 ccc
3.2 容量
容量 | 作用 |
---|---|
empty | 检查底层的容器是否为空 |
size | 返回容纳的元素数 |
cout<<st.empty()<<endl; // 结果是 0
cout<<st.size()<<endl; // 结果是 3
3.3 修改函数
修改函数 | 作用 |
---|---|
push | 向栈顶插入元素 |
pop | 删除栈顶元素 |
st.push("ddd");
cout<<st.top()<<endl; // 结果是 ddd
st.pop();
cout<<st.top()<<endl; // 结果是 ccc