I'm writing a class called "MultiStack". It is a stack container in
which the elements can be of different types, for example you can push
an 'int' onto the stack, and then push a 'std::string' onto the stack.
This class shall have an interface as close as possible to "std::stack"
while accommodating new features. The following 7 public member
functions shall be implemented:
empty - Test whether container is empty
size - Return quanity of elements
pop - Remove top element
swap - Swap contents with another Multistack
push - Insert element
emplace - Construct and insert element
top - Access top element
Specifically the following four member functions will have the same
signature and behave exactly as they do for "std::stack":
empty, size, pop, swap
However, whereas the standard library container "std::stack" contains elements of only one type, for example:
std::stack<int > my_multistack_of_ints ;
std::stack<float > my_multistack_of_floats ;
std::stack<std::string> my_multistack_of_strings;
The MultiStack container can have a unique type for each element,
for example:
int my_int = 5;
double my_double = 67.45;
std::string my_str("I like green monkeys");
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .
I'm writing a class called "MultiStack". It is a stack container in
which the elements can be of different types, for example you can push
an 'int' onto the stack, and then push a 'std::string' onto the stack.
So now the only member function left to implement is "top". At first
glance, you might think that the way to implement "top" would be to have multiple overloads of the member function "top", but C++ doesn't allow function overloads that only differ by return type. The following is disallowed:
class MultiStack {
public:
int top(void) { /* implementation */ }
float top(void) { /* implementation */ }
std::string top(void) { /* implementation */ }
};
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .
Am 05.11.2021 um 00:40 schrieb Öö Tiib:
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .Any allocates its items usually on the heap because there is
no maximum storage requirement of the items, whereas variant
is internally usually sth. like a union and a type tag, which
might consume more memory, but is significantly faster.
On Friday, 5 November 2021 at 19:48:15 UTC, Bonita Montero wrote:
Am 05.11.2021 um 00:40 schrieb Öö Tiib:
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:Any allocates its items usually on the heap because there is
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .
no maximum storage requirement of the items, whereas variant
is internally usually sth. like a union and a type tag, which
might consume more memory, but is significantly faster.
I assume you’re talking about std::variant. It is not internally using south, whatever you think.
On Friday, 5 November 2021 at 19:48:15 UTC, Bonita Montero wrote:
Am 05.11.2021 um 00:40 schrieb Öö Tiib:
On Thursday, 4 November 2021 at 23:05:59 UTC+2, David Brown wrote:Any allocates its items usually on the heap because there is
On 04/11/2021 12:45, Frederick Gotham wrote:
I'm writing a class called "MultiStack". It is a stack container in
...
How does it compare to:
std::stack<std::variant<int, float, std::string>> my_stack;
His idea seems closer to std::stack<std::any> .
no maximum storage requirement of the items, whereas variant
is internally usually sth. like a union and a type tag, which
might consume more memory, but is significantly faster.
I assume you’re talking about std::variant. It is not internally using south, whatever you think.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 185 |
Nodes: | 16 (1 / 15) |
Uptime: | 169:01:42 |
Calls: | 3,766 |
Calls today: | 4 |
Files: | 11,191 |
Messages: | 3,474,219 |