本文整理汇总了C++中stack::swap方法的典型用法代码示例。如果您正苦于以下问题:C++ stack::swap方法的具体用法?C++ stack::swap怎么用?C++ stack::swap使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类stack
的用法示例。
在下文中一共展示了stack::swap方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: postorderTraversal
vector<int> postorderTraversal(TreeNode *root) {
vector<int> answer;
stack<stackNode> emptyStack;
fullStack.swap(emptyStack);
if (root == NULL)
{
return answer;
}
fullStack.push(stackNode(root, 0));
while(!fullStack.empty())
{
stackNode currNode = fullStack.top();
fullStack.pop();
if (currNode.state == 0)
{
fullStack.push(stackNode(currNode.node, 1));
if (currNode.node->right != NULL)
{
fullStack.push(stackNode(currNode.node->right, 0));
}
if (currNode.node->left != NULL)
{
fullStack.push(stackNode(currNode.node->left, 0));
}
}
else
{
answer.push_back(currNode.node->val);
}
}
return answer;
}
示例2: sort_stack
void sort_stack(stack<int>& s) {
stack<int> r;
while (!s.empty()) {
int tmp = s.top();
s.pop();
while (!r.empty() && r.top() < tmp) {
s.push(r.top());
r.pop();
}
r.push(tmp);
}
s.swap(r);
}
示例3: sort_stack
void sort_stack(stack<T>& s)
{
stack<T> helper;
while (s.size() > 0) {
T t = s.top();
s.pop();
if (helper.empty() || t >= helper.top()) {
helper.push(t);
continue;
}
while (helper.size() > 0) {
T h = helper.top();
if (t >= h) {
break;
}
s.push(h);
helper.pop();
}
helper.push(t);
}
s.swap(helper);
}
示例4: maxDepth
int maxDepth(TreeNode *root) {
// Note: The Solution object is instantiated only once and is reused by each test case.
stack<StateNode> tmp;
treeStack.swap(tmp);
int maxHeight = 0;
if (root != NULL)
{
StateNode initState;
initState.height = 1;
initState.treeNode = *root;
treeStack.push(initState);
while(!treeStack.empty())
{
StateNode sNode = treeStack.top();
if (sNode.height > maxHeight)
{
maxHeight = sNode.height;
}
treeStack.pop();
if (sNode.treeNode.left != NULL)
{
StateNode newNode;
newNode.height = sNode.height + 1;
newNode.treeNode = *sNode.treeNode.left;
treeStack.push(newNode);
}
if (sNode.treeNode.right != NULL)
{
StateNode newNode;
newNode.height = sNode.height + 1;
newNode.treeNode = *sNode.treeNode.right;
treeStack.push(newNode);
}
}
}
return maxHeight;
}