C++篇-STL

C++篇-STL

wensboy Lv2

Preface

When it comes to data structures and algorithms,C++’s standard template library can greatly help you speed up your efficiency. However, the standard template library is a very large library, which contains many encapsulated data structure templates and algorithm implementations, so it is actually a bit difficult to memorize completely, and it is almost difficult to implement, which may be an NP problem. Yes, it seems as if our memories are always limited. Therefore, a good memory is not as good as the memory of a pen, and recording this learning process is also a process of continuous memory.

Guide

About STL

Something about C++ stl

feature of STL

STL encompasses the following core concepts

contain of STL

Imitation functions
Algorithm
Iterator
Space Configurator
Containers
Adapters

Learning priorities are given here:

  • Containers
  • Algorithm
  • Iterator
  • Adapters
  • Space Configurator
  • Imitation functions

note that this does not necessarily apply to all developers who are learning advanced features of C++. It’s a good habit to study selectively according to your needs.

Containers

collection of data structures

containers

Before starting learning containers, you need to add some pre-knowledge such as the use of std::pair, and at the same time, it will also add some extended knowledge in the continuous recording process.

std::pair

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
template<class T1,class T2> struct pair{}
first_type --> T1
second_type --> T2

eg:
pair<int,int> first_pair = {1,2};
/*
first_pair.first --> 1
first_pair.second --> 2
*/

/*functions*/

//std::make_pair()
auto p = std::make_pair(T1,T2);

//std::swap()
auto p1 = std::make_pair(10,10);
auto p2 = std::pair(20,9.9);

p1.swap(p2);
/*
p1-->{20,9.9}
p2-->{10,10}
*/

std::swap(p1,p2);
/*
p1-->{10,10}
p2-->{20,9.9}
*/

//std::get(std:pair)
auto p =std::make_pair(9,3.14);
std::get<0>(p);
std::get<1>(p);
std::get<int>(p);
std::get<double>(p);
/*
0 --> first
1 --> second
int --> int_type
double --> double_type
*/

//logic operators
from 0 --> 1 to compare:
auto p1,p2;
if p1>p2 :
print std::get<0>(p1)>std::get<0>(p2) and std::get<1>(p1)>std::get<1>(p2);

std::map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
//create a map with two pairs
std::map<class Key ,class T> m{{"cpu",1},{"gpu",2}};

//access the m
for(const auto& [key,value] : m)
{
std::cout<<"[ " << key << ":" << value << std::endl;
}
/*
[ "cpu" : 1]
[ "gpu" : 2]
*/

//update old pair and add new pair
m["cpu"] = 3;
m["ram"] = 4;
/*
cpu:3
ram:4
*/

//erase a pair
m.erase("key")

//get the size of the map
m.size()

//clear up the map
m.clear()

//if not empty
m.empty()

Summary

Reference

  • Title: C++篇-STL
  • Author: wensboy
  • Created at : 2024-11-28 06:22:59
  • Updated at : 2024-11-28 06:22:59
  • Link: https://www.wensboy.site/2024/11/28/cplusplus-STL/
  • License: This work is licensed under CC BY-NC-SA 4.0.
Comments