Namespaces
Variants
Actions

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

From cppreference.com
< cpp‎ | container‎ | stack
 
 
 
 
void push( const value_type& value );
(1)
void push( value_type&& value );
(2) (since C++11)

Pushes the given element value to the top of the stack.

1) Equivalent to: c.push_back(value).
2) Equivalent to: c.push_back(std::move(value)).

[edit] Parameters

value - the value of the element to push

[edit] Return value

(none)

[edit] Complexity

Equal to the complexity of Container::push_back.

[edit] Example

This program implements the BrainHack DSL, when the use of std::stack is an idiomatic way to process paired brackets.

Run this code
#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;
 std::array <std::uint8_t, 32768> data_{0};
 unsigned program_{0};
 int pos_{0};
 
 void collect_brackets(const std::string_view program)
 {
 std::stack <unsigned> brackets_stack;
 
 for (auto pos{0U}; pos != program.length(); ++pos)
 {
 if (const char c{program[pos]}; '[' == 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 or static_cast<int>(data_.size()) <= pos)
 throw std::out_of_range {"Data pointer out of bound!"};
 }
 
public:
 BrainHackInterpreter(const std::string_view program)
 {
 for (collect_brackets(program); program_ < program.length(); ++program_)
 switch (program[program_])
 {
 case '<':
 check_data_pos(--pos_);
 break;
 case '>':
 check_data_pos(++pos_);
 break;
 case '-':
 --data_[pos_];
 break;
 case '+':
 ++data_[pos_];
 break;
 case '.':
 std::cout << data_[pos_];
 break;
 case ',':
 std::cin >> data_[pos_];
 break;
 case '[':
 if (data_[pos_] == 0)
 program_ = open_brackets[program_];
 break;
 case ']':
 if (data_[pos_] != 0)
 program_ = close_brackets[program_];
 break;
 }
 }
};
 
int main()
{
 BrainHackInterpreter
 {
 "++++++++[>++>>++>++++>++++<<<<<-]>[<+++>>+++<-]>[<+"
 "+>>>+<<-]<[>+>+<<-]>>>--------.<<+++++++++.<<----.>"
 ">>>>.<<<------.>..++.<++.+.-.>.<.>----.<--.++.>>>+."
 };
 std::cout << '\n';
}

Output:

Hi, cppreference!

[edit] See also

(C++11)
constructs element in-place at the top
(public member function) [edit]
removes the top element
(public member function) [edit]
Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/container/stack/push&oldid=122472"

AltStyle によって変換されたページ (->オリジナル) /