Quick Links: |
Provide an STL-compliant stack class. More...
Classes | |
class | bsl::stack< VALUE, CONTAINER > |
Typedefs | |
typedef CONTAINER::value_type | bsl::stack::value_type |
typedef CONTAINER::reference | bsl::stack::reference |
typedef CONTAINER::const_reference | bsl::stack::const_reference |
typedef CONTAINER::size_type | bsl::stack::size_type |
typedef CONTAINER | bsl::stack::container_type |
Functions | |
bsl::stack::BSLMF_NESTED_TRAIT_DECLARATION_IF (stack, BloombergLP::bslma::UsesBslmaAllocator, BloombergLP::bslma::UsesBslmaAllocator< container_type >::value) | |
bsl::stack::stack () | |
bsl::stack::stack (const stack &original) | |
bsl::stack::stack (BloombergLP::bslmf::MovableRef< stack > original) | |
bsl::stack::stack (const CONTAINER &container) | |
bsl::stack::stack (BloombergLP::bslmf::MovableRef< CONTAINER > container) | |
template<class ALLOCATOR > | |
bsl::stack::stack (const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::stack::stack (const CONTAINER &container, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::stack::stack (const stack &original, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::stack::stack (BloombergLP::bslmf::MovableRef< CONTAINER > container, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
template<class ALLOCATOR > | |
bsl::stack::stack (BloombergLP::bslmf::MovableRef< stack > original, const ALLOCATOR &basicAllocator, typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type *=0) | |
stack & | bsl::stack::operator= (const stack &rhs) |
stack & | bsl::stack::operator= (BloombergLP::bslmf::MovableRef< stack > rhs) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(false) |
template<class... Args> | |
reference | bsl::stack::emplace (Args &&...args) |
void | bsl::stack::pop () |
void | bsl::stack::push (const value_type &value) |
void | bsl::stack::push (BloombergLP::bslmf::MovableRef< value_type > value) |
bool | bsl::stack::empty () const |
size_type | bsl::stack::size () const |
const_reference | bsl::stack::top () const |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator== (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator!= (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator< (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator> (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator<= (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
bool | bsl::operator>= (const stack< VALUE, CONTAINER > &lhs, const stack< VALUE, CONTAINER > &rhs) |
template<class VALUE , class CONTAINER > | |
void | bsl::swap (stack< VALUE, CONTAINER > &lhs, stack< VALUE, CONTAINER > &rhs) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(false) |
Variables | |
container_type | bsl::stack::c |
void swap(stack &other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION(bsl reference | bsl::stack::top () |
Friends | |
template<class VAL , class CONT > | |
bool | bsl::stack::operator== (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
template<class VAL , class CONT > | |
bool | bsl::stack::operator!= (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
template<class VAL , class CONT > | |
bool | bsl::stack::operator< (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
template<class VAL , class CONT > | |
bool | bsl::stack::operator> (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
template<class VAL , class CONT > | |
bool | bsl::stack::operator<= (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
template<class VAL , class CONT > | |
bool | bsl::stack::operator>= (const stack< VAL, CONT > &, const stack< VAL, CONT > &) |
bsl::stack | STL-compliant stack template |
bsl::stack
, a container adapter that takes an underlying container and provides a stack interface which the user accesses primarily through push
, pop
, and top
operations. A deque
(the default), vector
, or list
may be used, but any container which supports push_back
, pop_back
, back
, and size
, plus a template specialization uses_allocator::type
, may be used. stack
implemented here adheres to the C++11 standard when compiled with a C++11 compiler, and makes the best approximation when compiled with a C++03 compiler. In particular, for C++03 we emulate move semantics, but limit forwarding (in emplace
) to const
lvalues, and make no effort to emulate noexcept
or initializer-lists. bsl::stack
adapter can accept for its (optional) CONTAINER
template parameter bsl::deque
(the default), bsl::vector
, bsl::list
, or other container classes that support the following types and methods. value_type
reference
const_reference
size_type
allocator_type
(if any stack
constructor taking an allocator is used) void push_back(const value_type&)
(and variant taking rvalue reference) void pop_back()
reference back()
size_type size() const
const_reference back() const
emplace_back
==
, !=
, <
, >
, <=
, >=
operators swap
function (found via ADL with std::swap
in the lookup set) CONTAINER
template parameter is omitted the VALUE
template parameter specifies the value_type
of bsl::vector
, the default container type. The VALUE
template has no other role. CONTAINER::value_type
determines the contained value type and VALUE
is simply ignored. The resulting code may work with instances of VALUE
(e.g., VALUE
is convertible to CONTAINER::value_type
) or not (compiler errors). CONTAINER::allocator_type
exists, and if it does exist it is assumed to be the allocator type used by CONTAINER
, and that CONTAINER
supports constructors of this type. CONTAINER::allocator_type
type, and allocators of this type are propagated to all constructors of the underlying container. In the case of container types bsl::deque
(the default type), bsl::vector
, and bsl::list
, CONTAINER::allocator_type
is bsl::allocator
which is implicitly convertible from bslma::Allocator *
, and which can be converted to a bslma::Allocator *
through the mechanism
accessor. bsl::allocator
, then the stack
object can take bslma_Allocator *
s to supply memory allocation. If no allocator is specified, allocator()
is used, which winds up using bslma::Default::allocator(0)
. bsl::stack
class that effectively forward their implementations to corresponding operations in the held container (referenced as c
) which is here assumed to be either bsl::deque
, or bsl::vector
, or bsl::list
. Legend ------ 'C' - (template parameter) type 'CONTAINER' of the stack 'V' - (template parameter) type 'VALUE' of the stack 's', 't' - two distinct objects of type 'stack<V, C>' 'nc' - number of elements in container 'c' 'n', 'm' - number of elements in 's' and 't', respectively 'al' - STL-style memory allocator 'v' - an object of type 'V' +----------------------------------------------------+--------------------+ | Note: the following estimations of operation complexity assume the | | underlying container is a 'bsl::deque', 'bsl::vector', or 'bsl::list'. | +----------------------------------------------------+--------------------+ | Operation | Complexity | +====================================================+====================+ | stack<V, C> s; (default construction) | O(1) | | stack<V, C> s(al); | | +----------------------------------------------------+--------------------+ | stack<V, C> s(c); | O(nc) | | stack<V, C> s(c, al); | | +----------------------------------------------------+--------------------+ | stack<V, C> s(t); | O(n) | | stack<V, C> s(t, al); | | +----------------------------------------------------+--------------------+ | s.~stack(V, C>(); (destruction) | O(n) | +----------------------------------------------------+--------------------+ | s = t; (assignment) | O(n) | +----------------------------------------------------+--------------------+ | s.push(v) | O(1) | +----------------------------------------------------+--------------------+ | s.pop() | O(1) | +----------------------------------------------------+--------------------+ | s.top() | O(1) | +----------------------------------------------------+--------------------+ | s == t, s != t | O(n) | +---------------------------------------------------+--------------------+ | s < t, s <= t, s > t, s >= t | O(n) | +----------------------------------------------------+--------------------+ | s.swap(t), swap(s,t) | depends on the | | | container; for | | | deque, vector, and | | | list: | | | O(1) if 's' and | | | 't' use the same | | | allocator, | | | O(n + m) otherwise | +----------------------------------------------------+--------------------+ | s.size() | O(1) if 'C' is | | | deque or vector | +----------------------------------------------------+--------------------+ | s.empty() | O(1) | +----------------------------------------------------+--------------------+
class ToDoList { // DATA bsl::stack<const char *> d_stack; public: // MANIPULATORS void enqueueTask(const char *task); // Add the specified 'task', a string describing a task, to the // list. Note the lifetime of the string referred to by 'task' // must exceed the lifetime of the task in this list. bool finishTask(); // Remove the current task from the list. Return 'true' if a task // was removed and it was the last task on the list, and return // 'false' otherwise. // ACCESSORS const char *currentTask() const; // Return the string representing the current task. If there // is no current task, return the string "<EMPTY>", which is // not a valid task. }; // MANIPULATORS void ToDoList::enqueueTask(const char *task) { d_stack.push(task); } bool ToDoList::finishTask() { if (!d_stack.empty()) { d_stack.pop(); return d_stack.empty(); } return false; }; // ACCESSORS const char *ToDoList::currentTask() const { if (d_stack.empty()) { return "<EMPTY>"; } return d_stack.top(); }
ToDoList
. ToDoList toDoList;
toDoList.enqueueTask("Change the car's oil."); toDoList.enqueueTask("Pay the bills.");
assert(!strcmp("Pay the bills.", toDoList.currentTask()));
assert(false == toDoList.finishTask());
assert(!strcmp("Change the car's oil.", toDoList.currentTask()));
toDoList.enqueueTask("Get some hot dogs."); assert(!strcmp("Get some hot dogs.", toDoList.currentTask()));
assert(false == toDoList.finishTask()); assert(!strcmp("Change the car's oil.", toDoList.currentTask()));
assert(true == toDoList.finishTask()); assert(!strcmp("<EMPTY>", toDoList.currentTask()));
toDoList.enqueueTask("Clean the rain gutters.");
typedef CONTAINER::value_type bsl::stack< VALUE, CONTAINER >::value_type [inherited] |
typedef CONTAINER::reference bsl::stack< VALUE, CONTAINER >::reference [inherited] |
typedef CONTAINER::const_reference bsl::stack< VALUE, CONTAINER >::const_reference [inherited] |
typedef CONTAINER::size_type bsl::stack< VALUE, CONTAINER >::size_type [inherited] |
typedef CONTAINER bsl::stack< VALUE, CONTAINER >::container_type [inherited] |
bsl::stack< VALUE, CONTAINER >::BSLMF_NESTED_TRAIT_DECLARATION_IF | ( | stack< VALUE, CONTAINER > | , | |
BloombergLP::bslma::UsesBslmaAllocator | , | |||
BloombergLP::bslma::UsesBslmaAllocator< container_type >::value | ||||
) | [inherited] |
bsl::stack< VALUE, CONTAINER >::stack | ( | ) | [explicit, inherited] |
Create an empty stack. No allocator will be provided to the underlying container. That container's memory allocation will be provided by the default allocator of its type.
bsl::stack< VALUE, CONTAINER >::stack | ( | const stack< VALUE, CONTAINER > & | original | ) | [inherited] |
Create a stack having the value of the specified original
. The currently installed default allocator is used to supply memory.
bsl::stack< VALUE, CONTAINER >::stack | ( | BloombergLP::bslmf::MovableRef< stack< VALUE, CONTAINER > > | original | ) | [inherited] |
Create a stack having the value of the specified original
by moving the contents of original
to the new stack. The allocator associated with original
is propagated for use in the new stack. original
is left in a valid but unspecified state.
bsl::stack< VALUE, CONTAINER >::stack | ( | const CONTAINER & | container | ) | [explicit, inherited] |
Create a stack whose underlying container has the value of the specified container
. The currently installed default allocator is used to supply memory.
bsl::stack< VALUE, CONTAINER >::stack | ( | BloombergLP::bslmf::MovableRef< CONTAINER > | container | ) | [explicit, inherited] |
Create a stack whose underlying container has the value of the specified container
(on entry) by moving the contents of container
to the new stack. The allocator associated with container
is propagated for use in the new stack. container
is left in a valid but unspecified state.
bsl::stack< VALUE, CONTAINER >::stack | ( | const ALLOCATOR & | basicAllocator, | |
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [explicit, inherited] |
Create an empty stack, and use the specified basicAllocator
to supply memory. If CONTAINER::allocator_type
does not exist, this constructor may not be used.
bsl::stack< VALUE, CONTAINER >::stack | ( | const CONTAINER & | container, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a stack whose underlying container has the value of the specified container
, and use the specified basicAllocator
to supply memory. If CONTAINER::allocator_type
does not exist, this constructor may not be used.
bsl::stack< VALUE, CONTAINER >::stack | ( | const stack< VALUE, CONTAINER > & | original, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a stack having the value of the specified stack original
and use the specified basicAllocator
to supply memory. If CONTAINER::allocator_type
does not exist, this constructor may not be used.
bsl::stack< VALUE, CONTAINER >::stack | ( | BloombergLP::bslmf::MovableRef< CONTAINER > | container, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a stack whose underlying container has the value of the specified container
(on entry) that uses basicAllocator
to supply memory by using the allocator-extended move constructor of CONTAINER. 'container
is left in a valid but unspecified state. A bslma::Allocator *
can be supplied for basicAllocator
if the (template parameter) ALLOCATOR
is bsl::allocator
(the default). This method assumes that CONTAINER
has a move constructor. If CONTAINER::allocator_type
does not exist, this constructor may not be used.
bsl::stack< VALUE, CONTAINER >::stack | ( | BloombergLP::bslmf::MovableRef< stack< VALUE, CONTAINER > > | original, | |
const ALLOCATOR & | basicAllocator, | |||
typename enable_if< bsl::uses_allocator< CONTAINER, ALLOCATOR >::value, ALLOCATOR >::type * | = 0 | |||
) | [inherited] |
Create a stack having the value of the specified original
(on entry) that uses basicAllocator
to supply memory by using the allocator-extended moved constructor of CONTAINER
. original
is left in a valid but unspecified state. Note that a bslma::Allocator *
can be supplied for basicAllocator
if the (template parameter) ALLOCATOR
is bsl::allocator
(the default). Also note that this method assumes that CONTAINER
has a move constructor. Also note that if CONTAINER::allocator_type
does not exist, this constructor may not be used.
stack& bsl::stack< VALUE, CONTAINER >::operator= | ( | const stack< VALUE, CONTAINER > & | rhs | ) | [inherited] |
Assign to this object the value of the specified rhs
object, and return a reference providing modifiable access to this object.
stack& bsl::stack< VALUE, CONTAINER >::operator= | ( | BloombergLP::bslmf::MovableRef< stack< VALUE, CONTAINER > > | rhs | ) | [inherited] |
Assign to this object the value of the specified rhs
object, and return a reference providing modifiable access to this object. The contents of rhs
are moved to this stack using the move-assignment operator of CONTAINER
. rhs
is left in a valid but unspecified state, and if an exception is thrown, *this
is left in a valid but unspecified state.
reference bsl::stack< VALUE, CONTAINER >::emplace | ( | Args &&... | args | ) | [inherited] |
Push onto this stack a newly created value_type
object constructed by forwarding get_allocator()
(if required) and the specified (variable number of) args
to the corresponding constructor of value_type
. Return a reference providing modifiable access to the inserted element.
void bsl::stack< VALUE, CONTAINER >::pop | ( | ) | [inherited] |
Remove the top element from this stack. The behavior is undefined if this stack is empty.
void bsl::stack< VALUE, CONTAINER >::push | ( | const value_type & | value | ) | [inherited] |
Push the specified value
onto the top of this stack.
void bsl::stack< VALUE, CONTAINER >::push | ( | BloombergLP::bslmf::MovableRef< value_type > | value | ) | [inherited] |
Push onto this stack a value_type
object having the value of the specified value
(on entry) by moving the contents of value
to the new object on this stack. value
is left in a valid but unspecified state.
bool bsl::stack< VALUE, CONTAINER >::empty | ( | ) | const [inherited] |
Return true
if this stack contains no elements and false
otherwise.
size_type bsl::stack< VALUE, CONTAINER >::size | ( | ) | const [inherited] |
Return the number of elements contained in this stack.
const_reference bsl::stack< VALUE, CONTAINER >::top | ( | ) | const [inherited] |
Return a reference providing non-modifiable access to the element at the top of this stack. The behavior is undefined if the stack is empty.
bool bsl::operator== | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the specified lhs
and rhs
objects have the same value, and false
otherwise. Two stack
objects lhs
and rhs
have the same value if they have the same number of elements, and each element in the ordered sequence of elements of lhs
has the same value as the corresponding element in the ordered sequence of elements of rhs
. This method requires that the (template parameter) type VALUE
be equality-comparable
(see Requirements on VALUE
).
bool bsl::operator!= | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the specified lhs
and rhs
objects do not have the same value, and false
otherwise. Two stack
objects lhs
and rhs
do not have the same value if they do not have the same number of elements, or some element in the ordered sequence of elements of lhs
does not have the same value as the corresponding element in the ordered sequence of elements of rhs
. This method requires that the (template parameter) type VALUE
be equality-comparable
(see Requirements on VALUE
).
bool bsl::operator< | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the value of the specified lhs
stack is lexicographically less than that of the specified rhs
stack, and false
otherwise. Given iterators i
and j
over the respective sequences [lhs.begin() .. lhs.end())
and [rhs.begin() .. rhs.end())
, the value of stack lhs
is lexicographically less than that of stack rhs
if true == *i < *j
for the first pair of corresponding iterator positions where *i < *j
and *j < *i
are not both false
. If no such corresponding iterator position exists, the value of lhs
is lexicographically less than that of rhs
if lhs.size() < rhs.size()
. This method requires that operator<
, inducing a total order, be defined for value_type
.
bool bsl::operator> | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the value of the specified lhs
stack is lexicographically greater than that of the specified rhs
stack, and false
otherwise. The value of stack lhs
is lexicographically greater than that of stack rhs
if rhs
is lexicographically less than lhs
(see operator<
). This method requires that operator<
, inducing a total order, be defined for value_type
. Note that this operator returns rhs < lhs
.
bool bsl::operator<= | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the value of the specified lhs
stack is lexicographically less than or equal to that of the specified rhs
stack, and false
otherwise. The value of stack lhs
is lexicographically less than or equal to that of stack rhs
if rhs
is not lexicographically less than lhs
(see operator<
). This method requires that operator<
, inducing a total order, be defined for value_type
. Note that this operator returns !(rhs < lhs)
.
bool bsl::operator>= | ( | const stack< VALUE, CONTAINER > & | lhs, | |
const stack< VALUE, CONTAINER > & | rhs | |||
) |
Return true
if the value of the specified lhs
stack is lexicographically greater than or equal to that of the specified rhs
stack, and false
otherwise. The value of stack lhs
is lexicographically greater than or equal to that of stack rhs
if lhs
is not lexicographically less than rhs
(see operator<
). This method requires that operator<
, inducing a total order, be defined for value_type
. Note that this operator returns !(lhs < rhs)
.
void bsl::swap | ( | stack< VALUE, CONTAINER > & | lhs, | |
stack< VALUE, CONTAINER > & | rhs | |||
) |
Swap the value of the specified lhs
stack with the value of the specified rhs
stack.
container_type bsl::stack< VALUE, CONTAINER >::c [protected, inherited] |
We are required by the standard to have the
void swap (stack& other) BSLS_KEYWORD_NOEXCEPT_SPECIFICATION( bsl reference bsl::stack< VALUE, CONTAINER >::top() [inherited] |
< Exchange the value of this stack with the value of the specified other
stack. Return a reference to the element at the top of this stack. The behavior is undefined if this stack is empty.
bool operator== | ( | const stack< VAL, CONT > & | , | |
const stack< VAL, CONT > & | ||||
) | [friend, inherited] |
bool operator!= | ( | const stack< VAL, CONT > & | , | |
const stack< VAL, CONT > & | ||||
) | [friend, inherited] |
bool operator< | ( | const stack< VAL, CONT > & | , | |
const stack< VAL, CONT > & | ||||
) | [friend, inherited] |
bool operator> | ( | const stack< VAL, CONT > & | , | |
const stack< VAL, CONT > & | ||||
) | [friend, inherited] |
bool operator<= | ( | const stack< VAL, CONT > & | , | |
const stack< VAL, CONT > & | ||||
) | [friend, inherited] |