answersLogoWhite

0

An array in C++ is fixed-size and is unsuitable for implementing stacks. You can use a C-style dynamic array, of course, but the C++ method is to use a vector. A vector encapsulates a C-style dynamic array along with its size and greatly simplifies the way you work with arrays.

Dynamic arrays are generally the best way to implement a stack, however be aware that when the array is full and you want to add a new element, you must reallocate the array which can occasionally cause the entire array to be moved to new memory. A vector, on the other hand, automatically reserves 1.6 times the consumed memory on each reallocation and thus minimises the need to reallocate.

Vectors provide a push_back() and pop_back() method, since these are the fastest methods of inserting and extracting elements from an array (inserting or extracting anywhere else would result in elements being shunted up and down the array, which is inefficient). These two methods alone are all you need to implement a stack. However, vectors also permit random access to any element within the array. To eliminate all unnecessary features, you must encapsulate the vector within an adaptor class (a thin-wrapper) that only exposes the functionality you actually need. A minimal implementation is presented here:

#include<vector>

template<typename T>

class stack

{

std::vector<T> m_data;

public:

stack() =default;

~stack() =default;

stack(const stack&) =default;

stack(stack&&) =default;

stack& operator= (const stack&) =default;

stack& operator= (stack&&) =default;

void push (const T& data) {m_data.push_back (data);}

void pop () {m_data.pop_back ();}

T& top () {return m_data.back();}

const T& top () const {return m_data.back();}

};

This is fairly similar to the way a std::stack is implemented in the STL, although it also provides specialisations for pointer types. For more information on the implementation, consult the <stack> header in the standard library.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

How should you design a scientific calculator using C plus plus?

The easiest way to implement a calculator is an RPN calculator (enter the numbers first, perform the operation last). You need a last-in-first-out stack (there's a "stack" class in C++, but you can also implement your own using an array or a linked list), and a set of functions that pop the last elements from the stack and push the result (e.g. Add() pops the last 2 values and pushes their addition).You'll need the math.h library for scientific operations.


Representation of stack data structure in c plus plus?

Stack is an abstract data type that allows you to input and output data in a way that the first data which was placed in the stack will be the last one to get out. We use physical examples of stack in our daily lives such as the stack of dishes or stack of coins where you only add or remove objects from the top of the stack. You can see the implementation in c++ in related links, below.


Is it possibly to return an array of strings in a function without using pointers in C plus plus?

No.


How to write a code for finding sum of left diagonals elements in an array using c plus plus?

truzi i Ghal


In C plus plus is it possible to instantiate an array without specifying a length?

No. You can declare a dynamic array without specifying a length, but in order to physically instantiate (either by using malloc or by using object-oriented construction) you must provide a length.


Would you Write c plus plus program using array for Fibonacci number?

You can write a C++ fib pro using arrays but the problem is the prog becomes very complicated since u need to pass the next adding value in an array.....


Does PlayStation plus stack?

Yes, it is possible to purchase, or 'stack', additional time to your Plus membership


What is the lowest subscript of an array in c plus plus?

The lowest subscript of an array in C, or C++ is 0.


What is an ordered list of data structure using c plus plus?

An ordered list of data in any programming language is simply a sorted array or list. In C++ this can either mean a sorted array, vector, list or forward list.


Can you stack a Plus 4 card on top of a Plus 2 card in the game of Uno?

No, you cannot stack a Plus 4 card on top of a Plus 2 card in the game of Uno.


How you use stack in c plus plus classes?

It would be easier to manipulate the stack in assembly language rather than C++.


How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.