Simulation implementation of STL-stack

foreword

We have written the simulation implementation of string, vector, and list before. We can know that STL generics are helpful for us to understand even data structures that have never been used. The interface names are very similar, and they all belong to the six major components of STL. One of the "containers", and in this chapter, we will talk about the "adapter", one of the six major components in STL, and the stack (stack) is an adapter.

What is a stack?

There is a stack area in the C++ address space, which stores local variables, and the stack we are talking about today is not the stack area, but a data structure.

 The stack is well understood and follows a first-in, last-out (last in, first out) principle.

So why can stack be called "adapter" in STL? 

Let's first understand what an adapter is. Referring to reality, we have power adapters and charging adapters. What are their functions?

For example, our mobile phone charging adapter, you are at your friend's house, your mobile phone is out of power, and you want to use his charger to charge your mobile phone, but your mobile phone is Android, and your friend's mobile phone is Apple. So what to do? At this time, if your friend's house has a charging adapter, which can be plugged into the adapter to turn it into an Android charging port, that's fine. So the role of the adapter is like this. The STL adapter here is similar to this concept. It adapts to our container, such as vector. Let's talk about how it adapts in detail below.

Simulation implementation of Stack

template<typename T, class Container = deque<T> > //Container(容器)
class stack
{
private:
	Container _data;
};

The difference between it as an adapter and a container is that its template has an additional template of Container. This template type must be a container, and here it also gives a default parameter deque<T>. Deque is a double queue, which will not be expanded here. In detail, if you are interested, you can do your own research.

Looking at its member variables, it is a container object.

Let the container object be a member variable, you can think about it, what are the benefits?

This highlights the power of STL generic programming. If we need to implement the interface of stack, just call the
interface of _data, because it is generic programming, and each container has a strong commonality.

size_t size() const
{
	return _data.size();
}

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

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

bool empty() const
{
	return _data.empty();
}

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

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

This has written all the interfaces of the stack, isn't it super simple?

By directly implementing your own interface by calling the interface of the container, you can really feel the generics of STL.

So do we need to write its default member function ourselves?

You don’t have to, because Container must be a custom type, and the member functions generated by default (such as construction, destruction, copy) will inevitably call the default member functions of the custom type, so we don’t need to write it at all, unless you say you want to repeat If you want to load several constructors, then you need to write it yourself.

data structure

If here, our container is a vector, then it must be a stack composed of a continuous array.

Then, if the container input here is a list, then it will be a stack in the form of a doubly linked list.

An adapter is such an effect, it can adapt to the STL container that can achieve this effect, making its data structure diverse. However, the prerequisite for adaptation is that the container has a corresponding interface that can realize this function, otherwise an error will definitely be reported.

Summarize

The content of this article talks about the simulation implementation of the stack. More importantly, we have a preliminary contact with the adapter, one of the six major components of STL, and know the power of STL generics.

Guess you like

Origin blog.csdn.net/fengjunziya/article/details/130661210