命名空间
变体
操作

std::stack<T,Container>::push

来自 cppreference.com
< cpp‎ | container‎ | stack
void push( const value_type& value );
void push( value_type&& value );
(自 C++11 起)

将给定的元素 value 推入栈顶。

1) 实际上调用 c.push_back(value).
2) 实际上调用 c.push_back(std::move(value)).

内容

[编辑] 参数

value - 要推入的元素的值

[编辑] 返回值

(无)

[编辑] 复杂度

等于 Container::push_back 的复杂度。

[编辑] 示例

该程序实现了 BrainHack DSL,当使用 std::stack 是处理配对括号的惯用方式时。

#include <array>
#include <cstdint>
#include <iostream>
#include <map>
#include <stack>
#include <stdexcept>
#include <string_view>
 
class BrainHackInterpreter
{
    std::map<unsigned, unsigned> open_brackets, close_brackets;
    unsigned program_pos_{0};
    std::array<std::uint8_t, 32768> data_;
    int data_pos_{0};
 
    void collect_brackets_positions(const std::string_view program)
    {
        std::stack<unsigned> brackets_stack;
 
        for (auto pos{0U}; pos != program.length(); ++pos)
        {
            const char c{program[pos]};
            if ('[' == c)
                brackets_stack.push(pos);
            else if (']' == c)
            {
                if (brackets_stack.empty())
                    throw std::runtime_error("brackets [] do not match!");
                else
                {
                    open_brackets[brackets_stack.top()] = pos;
                    close_brackets[pos] = brackets_stack.top();
                    brackets_stack.pop();
                }
            }
        }
 
        if (!brackets_stack.empty())
            throw std::runtime_error("brackets [] do not match!");
    }
 
    void check_data_pos(int pos)
    {
        if (pos < 0 || pos >= static_cast<int>(data_.size()))
            throw std::out_of_range{"data pointer out of bound"};
    }
 
public:
    BrainHackInterpreter(const std::string_view program)
    {
        collect_brackets_positions(program);
        data_.fill(0);
 
        for (; program_pos_ < program.length(); ++program_pos_)
            switch (program[program_pos_])
            {
                case '<':
                    check_data_pos(--data_pos_);
                    break;
                case '>':
                    check_data_pos(++data_pos_);
                    break;
                case '-':
                    --data_[data_pos_];
                    break;
                case '+':
                    ++data_[data_pos_];
                    break;
                case '.':
                    std::cout << data_[data_pos_];
                    break;
                case ',':
                    std::cin >> data_[data_pos_];
                    break;
                case '[':
                    if (data_[data_pos_] == 0)
                        program_pos_ = open_brackets[program_pos_];
                    break;
                case ']':
                    if (data_[data_pos_] != 0)
                        program_pos_ = close_brackets[program_pos_];
                    break;
            }
    }
};
 
int main()
{
    BrainHackInterpreter
    {
        "++++++++[>++>>++>++++>++++<<<<<-]>[<+++>>+++<-]>[<+"
        "+>>>+<<-]<[>+>+<<-]>>>--------.<<+++++++++.<<----.>"
        ">>>>.<<<------.>..++.<++.+.-.>.<.>----.<--.++.>>>+."
    };
    std::cout << '\n';
}

输出

Hi, cppreference!

[编辑] 另请参见

(C++11)
在栈顶就地构造元素
(公共成员函数) [编辑]
移除栈顶元素
(公共成员函数) [编辑]