Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement STL Container for C++

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article is about how C++ implements the STL container. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

The characteristics of each container:

1. The containers that can be accessed with subscripts are (can be inserted or assigned): vector, deque, map

In particular, it is important to note that vector and deque cannot insert elements with subscripts if they do not specify the size in advance!

two。 Sequential containers can only be sized when the container is initialized, not relational containers.

3. Note that the iterator of the associated container does not support it+n operations, only it++ operations.

The concept of adapter

An adapter means to restrict or combine something that already exists into something new, which embodies some new features, but the underlying layer is implemented by something that already exists.

Containers in STL

Vector: vector (not mathematically) the simplest sequence type of STL and the default underlying class for some adapters

Deque: double-ended queues can be left and joined from beginning to end.

List: bidirectional linked list

Forwardd_list: one-way linked list, less function, can not be reversed.

Queue: queue, an adapter class (the underlying template class defaults to deque), which does not allow random access and traversal; shows the interface of the queue

Priority_queue: priority queue, an adapter class (the underlying template class defaults to vector), and the default large root heap (the largest element comes first).

Stack: stack, an adapter class (the underlying template class defaults to vector), which provides a typical stack interface to the underlying class.

Special

Array: not a STL container, fixed in length, but can also use some STL algorithms

Containers basically have the following functions, depending on the container.

Precinct qrem iPermie j represents iterator

Basic requirements of sequence, take vector as an example, p, Q, I, j represent iterator

Vector vec (NMague t); / / create and initialize vector (NMagnet); / / create anonymous objects and initialize vector vec (iMaginj); / / create and initialize to another container [iMaginj) content vector (iMaginj); / / create anonymous objects and initialize them to another container [iMaginj) content vec.insert (pMagnet) / insert vec.insert in front of t to p (pMagnec); / insert n vec.insert in front of t to p (ppenirej); / / insert interval [iPowerj) in front of p (which can be your own interval) vec.erase (p); / / delete the element vec.erase (ppenq) pointed to by p / / delete the element vec.clear () in the [pmaeq) range; / / empty the container, which is equivalent to vec.erase (vec.begin (), vec.end ())

Some optional requirements, see the name know the meaning, do not explain.

.front (); .back (); .push_front (); .push_back (); .pop_front (); .pop_back (); [n] .at (n)

.at () is very similar to [], but the former throws an exception when it crosses the boundary, which we can catch.

Deque

Demonstrate some of the above with a double-ended queue

# include # include # include using namespace std;void show (int & t) {cout

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report