In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the knowledge of "what is a C++ adaptive container". Many people will encounter this dilemma in the operation of actual cases, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Preface
The Standard template Library (STL) provides containers (such as stack, queue, priority_queue) that are used to simulate stack and queue behavior.
This kind of behavior that uses one container internally but presents another is called an adaptive container.
1. Behavioral characteristics of stacks and queues
Stacks and queues are very similar to arrays or list, but there are restrictions on how elements can be inserted, accessed, and deleted. Where the element can be inserted and where the element can be deleted determines the behavior of the container.
Stack: the stack is a first-in, last-out system, and elements can only be inserted or deleted from the top of the stack.
Queue: the queue is a first-in, first-out system in which elements are inserted at the end of the queue, deleted from the head of the queue, and the first inserted element is deleted first. The queue can be regarded as people standing in line to buy tickets at the cinema, and those who join the queue first leave first.
two。 Use the STL stack class
The generic STL container std::stack simulates this behavior of the stack. To use stack, you must include a header file:
# include
STL stack is a template class. Allows insertion and deletion of elements at the top, but does not allow access to the middle elements.
In the STL implementation, std::stack is defined as follows:
Templateclass stack
The parameter elementType is the object type stored by stack
The second template parameter, Container, is the default underlying container implementation class used by stack. The default is to use std::deque to store data, but you can specify to use vector or list to store data.
Instantiate stack
Create a stack to store elements of type double:
Stack stackDoubles
Create a stack that stores objects of a class, such as Tuna:
Stack stackTunas
Create a stack that stores double type elements and uses vector as the underlying container:
Stack::stackDoublesInVector
Use a copy of one stack object to create another stack object:
Stack stackIntsCopy (stackInts)
Member functions of stack
Stack changes the behavior of another container, such as deque, list, or vector, and implements its function by restricting the insertion or deletion of elements, thus providing behavioral characteristics that strictly adhere to the stack mechanism.
1.push
StackInts.push (25); / / insert elements at the top of the stack
2.pop
StackInts.pop (); / / Delete the elements at the top of the stack
3.empty
If (stackInts.empty ()) {DoSomething;} / / determines whether the stack is empty
4.size
Size_t nNum=stackInts.size (); / / returns the number of elements in the stack
5.top
StackInts.top (); / / get a reference to the element at the top of the stack
3. Use the STL queue class
STL queue is a template class. Only insert elements at the end and delete elements from the beginning are allowed; queue does not allow access to the middle elements, but can access the beginning and end elements.
The generic STL container std::queue simulates this behavior of queues. To use queue, you must include a header file:
# include
Std::queue is defined as follows:
Templateclass queue
The first parameter, elementType, is the type of element that the queue object contains.
The second parameter, Container, is the type of collection whose data is stored. It is deque by default, or can be set to list or vector.
Instantiate queue
Create a queue with a storage type of int:
Queue qIntegers
Create a storage element of type double and use std::list to store these elements:
Queue qDoublesInList
Instantiate another queue using one queue:
Queue qCopy (qIntegers)
Member functions of queue
1.push
QIntegers.push (25); / / insert an element at the end of the queue
2.pop
QIntegers.pop (); / / Delete the element of the line header
3.front
QIntegers.front (); / / returns a reference to the leader of the team
4.back
QIntegers.back (); / / returns a reference to the end of the line
5.empty
If (qIntegers.empty ()) {} / / check whether the queue is empty
6.size
Size_t nNum=qIntegers.size (); / / returns the number of elements in the queue
4. Use STL priority queue priority_queue
STL priority_queue is a template class, and you must also include a header file to use it:
# include
Priority_queue differs from queue in that the element that contains the maximum value is at the head of the line (by default) and can only be performed at the head of the line.
Instantiation
The std::priority_queue class is defined as follows:
Template class priority_queue
The third parameter, Compare, specifies a binary predicate. Std::less is used by default, sorted from largest to smallest, that is, the head of the queue is the maximum. Sort from smallest to largest using greater.
Instantiate an integer priority queue:
Priority_queue pqIntegers
Create a priority queue of element type double and stored in std::deque in order from smallest to largest:
Priority_queue pqIntegers_Inverse
Use one priority_queue to instantiate another priority_queue:
PqIntegers pqCopy (pqIntegers)
Member functions of priority_queue
1.push
PqIntegers.push (10); / / insert an element into the priority queue
2.pop
PqIntegers.pop (); / / Delete the queue header element, that is, the largest element
3.top
PqIntegers.top (); / / returns the reference of the first element in the queue (i.e. the largest element)
4.empty
If (pqIntegers.empty ()) {} / / check whether it is empty
5.size
PqIntegers.size (); / / returns the number of elements in the priority queue
This is the end of the content of "what is the C++ adaptive container". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.