C ++ container (III)

Source: Internet
Author: User

With a basic understanding of the data structure, it is easy to understand the concept of containers proposed by advanced languages. Next, let's take a look at C ++
Standard Template Library (STL)
Provided 10
Generic container.

What is container

First, we must understand what a container is.
Container is defined as: in data storage, there is an object type, it can hold other objects or pointer to other objects, this type of object is called container. The container is the object that saves other objects. Of course, this is a simple understanding. This "object" also contains a series of methods to process "other objects, because these methods are often used in program design, the container also shows a benefit, that is, "the container class is a good solution to specific code reuse problems ".

Another feature of containers is that containers can be expanded on their own. When solving the problem, we often don't know how many objects we need to store, that is, we don't know how much memory space we should create to save our objects. Obviously, arrays cannot work in this aspect. The advantage of a container is that you do not need to tell it in advance how many objects you want to store, as long as you create a container object and call the methods it provides reasonably, all the processing details will be completed by the container itself. It can apply for or release the memory for you and use the best algorithm to execute your commands.

With the advent of object-oriented language, containers are particularly important in object-oriented languages, and are even considered to be the basis of early object-oriented language. Currently, almost all object-oriented languages are accompanied by a container set.
Is the standard template library (STL
).

Unlike other languages, C ++
Processing containers in is based on templates. Standard C ++
Containers in the library provide a variety of data structures that can work well with standard algorithms, which provides good support for our software development!

General container classification

STL
The defined general containers are classified into three types: ordered containers, associated containers, and container adapters.

Ordered container


It is a linear table with sequential relationships between elements and a sequential cluster with a linear structure. Each element in the ordered container has a fixed position, unless it is changed by a delete or insert operation. This location has nothing to do with the element itself, but is related to the operation time and location. The ordered container does not sort the elements according to their characteristics, but directly stores the logical sequence of the element operation. For example, We append three elements to an ordered container at a time. The relative positions of these three elements in the container are consistent with the logical order of the append.

Associated container


Unlike ordered containers, Associative containers are non-linear tree structures. More accurately, they are binary tree structures. There is no strict physical order between elements, that is, the elements in the container do not save the logical order of elements placed in the container. However, the associative container provides another sorting function based on the characteristics of elements, so that the iterator can "sequentially" obtain elements based on the characteristics of the elements.

Another notable feature of an associated container is that it stores data in the form of key values, that is, it can associate keywords and values to save, the ordered container can only save one type (you can think that it only saves the keyword, or you can think that it only saves the value ). This can be described in the specific container class below.

Container Adapter


Is a relatively abstract concept, C ++
The adapter is a mechanism that makes the behavior of one thing look similar to the behavior of another thing. Container adapter is a mechanism that enables an existing container type to work in a different abstract type. In fact, only interface conversion occurs. You can understand it as a container. It is actually a container, but it does not depend on the specific standard container type. It can be understood as a container template. You can also understand it as the container interface, and the specific container type used by the adapter can be determined when you define the adapter.

The following table lists STL
The three types of containers defined include the following container classes:

Standard containers

Features

Ordered container

Vector

Quickly insert and delete elements from the backend to directly access any elements

Deque

You can quickly insert and delete elements from the front or back to directly access any element.

List

Double-stranded tables, fast insertion and deletion from anywhere

Associated container

Set

Quick search, repeated values not allowed

Multiset

Quick search, allowing repeated values

Map

One-to-multiple ing, fast search based on keywords, repeated values are not allowed

Multimap

One-to-multiple ing, fast search based on keywords, allowing repeated values

Container Adapter

Stack

Back-in-first-out

Queue

First-in-first-out

Priority_queue

The highest priority element is always the first column

From: blog.csdn.net/acossoft

 



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.