当前位置:网站首页>19.2 container classification, array and vector container refinement
19.2 container classification, array and vector container refinement
2022-06-28 18:42:00 【zzyzxb】
stl Component part
Containers 、 iterator 、 Algorithm ( function )、 distributor ( Allocate memory )、 other ( Adapter 、 functor / Function objects, etc ).
One : Classification of containers
vector、list、map, Containers are used to store data
STL Containers in can be divided into three categories
<1> Sequence containers (Sequence containers): Where to put it , Where this element is placed . such as :array、vector、deque、list、forward_list.
<2> Associate container (Associative Containers): Element is a key / It's worth it , Use the key (key) To find the value (value) Especially suitable for searching . Automatically sort internally by key , Be able to control the inserted content , However, the insertion position cannot be controlled . such as :set、multiset、map、multimap、hash_set、hash_map、hash_multiset、hash_multimap. Internal may use trees 、 Hash table .
<3> Unordered container (Unordered Containers):c++11 Launched in . The position of the element is not important , What matters is whether the element is in the container . Unordered containers are also related containers . With the insertion of elements , The position of each element in the container may change as it changes . such as :unordered_set、unordered_multiset、unordered_map、unordered_multimap. Internally, hash tables may be used to implement .

There is an official saying :c++ The standard does not stipulate that any container must use any specific means of implementation .
Two : Description of the container and examples of application matters
<1>array: Is a sequential container , It's actually an array , Memory space is continuous , The size is fixed ; How old were you when you first applied , He's just how old , No more size . In essence, it is an array with continuous memory space , Fixed size , You can't increase its size . Objects are continuous , But the memory space of the string pointed to by the object doesn't matter .
#include <array>
#include <string>
#include <iostream>
using namespace std;
void func()
{
// contain 5 Array of elements
array<string, 5> mystring = { "I", "Love1Love2Love3Love4Love5Love6Love7", "China" };
cout << "myString.size() = " << mystring.size() << endl; //5
mystring[0] = "It is very long~~~~~~~~~~~~~long~~~~~~~~~~~~long";
mystring[4] = "It is very long~~~~~~~~~~~~~long~~~~~~~~~~~~long";
cout << "sizeof(string) = " << sizeof(string) << endl;
for (size_t i = 0; i < mystring.size(); ++i)
{
const char* p = mystring[i].c_str();
cout << "-----------------begin---------------------" << endl;
cout << " Array element value = " << p << endl;
printf(" Object address = %p\n", &mystring[i]);
printf(" The string address to = %p\n", p);
cout << "-----------------end---------------------" << endl;
}
const char* p1 = "Love1Love2Love3Love4Love5Love6Love7";
const char* p2 = "Love1Love2Love3Love4Love5Love6Love7";
printf("p1 Address = %p\n", p1);
printf("p2 Address = %p\n", p2);
}
int main()
{
func();
return 0;
}
myString.size() = 5
sizeof(string) = 40
-----------------begin---------------------
Array element value = It is very long~~~~~~~~~~~~~long~~~~~~~~~~~~long
Object address = 000000244C30F9C0
The string address to = 0000018DE25E5CE0
-----------------end---------------------
-----------------begin---------------------
Array element value = Love1Love2Love3Love4Love5Love6Love7
Object address = 000000244C30F9E8
The string address to = 0000018DE25F1180
-----------------end---------------------
-----------------begin---------------------
Array element value = China
Object address = 000000244C30FA10
The string address to = 000000244C30FA18
-----------------end---------------------
-----------------begin---------------------
Array element value =
Object address = 000000244C30FA38
The string address to = 000000244C30FA40
-----------------end---------------------
-----------------begin---------------------
Array element value = It is very long~~~~~~~~~~~~~long~~~~~~~~~~~~long
Object address = 000000244C30FA60
The string address to = 0000018DE25E40B0
-----------------end---------------------
p1 Address = 00007FF655DAF398
p2 Address = 00007FF655DAF398

<2>vector
(1) Adding elements to the back and deleting elements from the back are both quick ;push_back().
(2) Inserting elements into the middle may cause many subsequent elements to be reconstructed , destructor . The efficiency will be very low .
(3) The search speed should not be too fast .
vector Container memory is also next to , vector The container has a “ Space ” The concept of , Each space can contain an element ;
How many elements in the container can be used size() Look at , And how much space there is in this container , Sure use capacity();
capacity() It must not be less than size();vector The number of spaces in the container must not be less than the number of elements ;
use reverse Space can be reserved , The premise is that you know in advance how many elements this container can hold ; It can greatly improve the running efficiency of the program .
边栏推荐
猜你喜欢

新工作第一天

EasyExcel 学习笔记

Small program graduation project based on wechat examination small program graduation project opening report function reference

Unity about oculus quest2 developing 002-ui interaction based on XR interaction Toolkit

What are the design requirements for PCB layout and wiring?

Common DOS commands

从知名软件提取出的神器,吊打一众付费

io模型初探

第2章 处理文件、摄像头和图形用户界面cameo应用

An error is reported when ActiveMQ is started. The 1883 port occupation problem is solved
随机推荐
匿名函数this指向以及变量提升
【云驻共创】昇腾异构计算架构CANN,助力释放硬件澎湃算力
BioVendor游离轻链(κ和λ)Elisa 试剂盒检测步骤
golang json 序列化、反序列化 字符串反序列化成 map[string]interface{}
Apifox 介绍
中金财富开户安全吗?开过中金财富的讲一下
好用、强大的PDF 阅读软件综合评测:PDF Expert 、MarginNote、LiquidText、Notability、GoodNotes、Zotero
闭包的理解
Lumiprobe 蛋白质标记研究方案
打破学科之间壁垒的STEAM教育
select/poll/epoll
CORBA 架构体系指南(通用对象请求代理体系架构)
io模型初探
About Covariance and Correlation(协方差和相关)
Analysis of response parsing process of SAP ui5 batch request
Huawei cloud AOM released version 2.0, and three features appeared
select/poll/epoll
剑指 Offer 11. 旋转数组的最小数字
Cann media data processing V2, jpegd interface introduction
19.2 容器分类、array、vector容器精解