创建带pair的stack
stack<pair<int,int>> s;
入栈操作
s.push({1,2});
出栈操作
s.pop();
读取栈顶的第一个,第二个元素
s.top().first;
s.top().second;
其他的基本功能,上个代码(从某英文网站上COPY的)
#include <bits/stdc++.h>
using namespace std;
// 打印栈顶内容
void printPair(pair<int, int> p)
{
cout << "("
<< p.first << ", "
<< p.second << ") ";
}
// 打印栈的所有内容
void Showstack(stack<pair<int, int> > s)
{
while (!s.empty()) {
printPair(s.top());
s.pop();
}
cout << '\n';
}
int main()
{
stack<pair<int, int> > s; //构造
s.push({ 10, 20 }); //入栈,注意是圆括号里面套花括号
s.push({ 15, 5 });
s.push({ 1, 5 });
s.push({ 5, 10 });
s.push({ 7, 9 });
cout << "Stack of Pairs: ";
Showstack(s); //展示栈内容
cout << "\nSize of Stack of Pairs: "
<< s.size(); //容量大小
cout << "\nTop of Stack of Pairs: ";
printPair(s.top()); //输出栈顶数据
cout << "\n\nRemoving the top pair\n";
s.pop(); //弹出栈顶数据
cout << "Current Stack of Pairs: ";
Showstack(s); //再次展示栈内容
return 0;
}
本人水平有限目前就想到这么多,欢迎补充