bool operator==(const stack&, const stack&) stack See below.
bool operator<(const stack&, const stack&) stack See below.
New members

These members are not defined in the Assignable and Default Constructible requirements, but are specific to stack.

Member Description
value_type The type of object stored in the stack. This is the same as T and Sequence::value_type.
size_type An unsigned integral type. This is the same as Sequence::size_type.
bool empty() const Returns true if the stack contains no elements, and false otherwise. S.empty() is equivalent to S.size() == 0.
size_type size() const Returns the number of elements contained in the stack.
value_type& top() Returns a mutable reference to the element at the top of the stack. Precondition: empty() is false.
const value_type& top() const Returns a const reference to the element at the top of the stack. Precondition: empty() is false.
void push(const value_type& x) Inserts x at the top of the stack. Postconditions: size() will be incremented by 1, and top() will be equal to x.
void pop() Removes the element at the top of the stack. [3] Precondition: empty() is false. Postcondition: size() will be decremented by 1.
bool operator==(const stack&, const stack&) Compares two stacks for equality. Two stacks are equal if they contain the same number of elements and if they are equal element-by-element. This is a global function, not a member function.
bool operator<(const stack&, const stack&) Lexicographical ordering of two stacks. This is a global function, not a member function.
Notes

[1] Stacks are a standard data structure, and are discussed in all algorithm books. See, for example, section 2.2.1 of Knuth. (D. E. Knuth, The Art of Computer Programming. Volume 1: Fundamental Algorithms, second edition. Addison-Wesley, 1973.)

[2] This restriction is the only reason for stack to exist at all. Note that any Front Insertion Sequence or Back Insertion Sequence can be used as a stack; in the case of vector, for example, the stack operations are the member functions back, push_back, and pop_back. The only reason to use the container adaptor stack instead is to make it clear that you are performing only stack operations, and no other operations.

[3] One might wonder why pop() returns void, instead of value_type. That is, why must one use top() and pop() to examine and remove the top element, instead of combining the two in a single member function? In fact, there is a good reason for this design. If pop() returned the top element, it would have to return by value rather than by reference: return by reference would create a dangling pointer. Return by value, however, is inefficient: it involves at least one redundant copy constructor call. Since it is impossible for pop() to return a value in such a way as to be both efficient and correct, it is more sensible for it to return no value at all and to require clients to use top() to inspect the value at the top of the stack.

See also

queue, priority_queue, Container, Sequence

Добавить отзыв
ВСЕ ОТЗЫВЫ О КНИГЕ В ИЗБРАННОЕ

0

Вы можете отметить интересные вам фрагменты текста, которые будут доступны по уникальной ссылке в адресной строке браузера.

Отметить Добавить цитату