Created
February 22, 2015 02:27
-
-
Save willkill07/d6eaf1e74fa586627502 to your computer and use it in GitHub Desktop.
C++11 stack implemented with std::forward_list
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <forward_list> | |
template <typename Type, typename Allocator = std::allocator <Type>, typename SizeType = std::size_t> | |
class | |
stack | |
{ | |
public: | |
using value_type = Type; | |
using allocator_type = Allocator; | |
using reference = value_type &; | |
using const_reference = const value_type &; | |
using pointer = typename std::allocator_traits <allocator_type>::pointer; | |
using const_pointer = typename std::allocator_traits <allocator_type>::const_pointer; | |
using size_type = SizeType; | |
private: | |
std::forward_list <Type> _list; | |
SizeType _size; | |
public: | |
explicit stack (const Allocator & alloc = Allocator()) | |
: _list {alloc}, _size {0} {} | |
explicit stack (const stack & other, const Allocator & alloc = Allocator()) | |
: _list {other.list, alloc}, _size {other.size} {} | |
stack (stack && x, const Allocator & alloc = Allocator()) | |
: _list {x.list, alloc}, _size {x.size} {} | |
template <typename ... Args> | |
void | |
emplace (Args && ... args) { ++_size; _list.emplace_front (args...); } | |
bool | |
empty() const { return size; } | |
void | |
pop() { --_size; _list.pop_front (); } | |
void | |
push (const_reference val) { ++_size; _list.push_front (val); } | |
void | |
push (value_type && val) { ++_size; _list.push_front (val); } | |
SizeType | |
size () const { return _size; } | |
void | |
swap (stack & x) { std::swap (_list, x._list); std::swap (_size, x._size); } | |
reference | |
top () { return _list.front(); } | |
const_reference | |
top () const { return _list.front(); } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment