C ++ learning 14- Introduction to different data structures of containers

Containers are divided into order / association / ...

One of the C ++ STL components: container
order container

  • vector one-dimensional array push_back / pop_back
  • deque double-ended queue push_back / pop_back push_front / pop_front
  • list doubly circular linked list container push_back / pop_back push_front / pop_front

Container Adapter: No iterator, no own data structure,
no other method, no other method, no other data structure, it is a tool class

  • stack 栈 push pop top size empty
  • queue 队列 push pop front back size empty
  • priority_queue big root heap / small root heap push pop top size empty

Associated container
set collection (key) map mapping table (key-value)
Unordered associative container(The time complexity of hash table addition and deletion check O (1))
Unordered associated container = "chained hash table
unordered_set
unordered_map

Ordered associative container(Red and black tree addition and deletion check time complexity O (logn))
Orderly associated container = "Red and black tree
set
map

Near container
char array [100]; char * p
string str;
bitset bit container
(array is also called container string also called container near container)
bitset bit container (accounting for size is not 0 or 1)

Sequential container:

容器交换函数  swap 效率怎么样?
直接对指针进行交换(地址的交换) 效率高  没有对内存的开辟构造释放
PS:array是内存不变数据变

1.vector: vector container

2x expanded one-dimensional array
push_back O (1) insert (it, val) O (n)
pop_back O (1) erase (it) O (n)
tail delete o (1) head delete O (n)
iterator traversal O (n)
operator [] random access O (1)

Efficiency-related methods
reserve (10) resize (10) —both will open up space but reserve only opens up memory without
empty size front back…

vector<int> vec;
vector<vector<int>> vec;  二维数组数据结构的容器开辟
vector<list<int>> vec;
list<vector<int>> vec;

2.deque: double-ended queue container

deque容器为一个给定类型的元素进行线性处理,像向量一样,
它能够快速地随机访问任一个元素,并且能够高效地插入和删
除容器的尾部元素。但它又与vector不同,deque支持高效插入
和删除容器的头部元素,因此也叫做双端队列。

Dynamically developed two-dimensional array
push_back O (1) push_front O (1) insert (it, val) O (n)
pop_back O (1) pop_front O (1) erase (it) O (n)
iterator traverse O (n )

When expanding the capacity, the first dimension is doubled by default. The second dimension is placed in the first dimension. The
Insert picture description here
memory of the first dimension expansion is not continuous, so the use efficiency of deque is higher than that of vector. Head and tail are inserted as fast as two, but when inserted from the middle Faster
empty size front back ...

3.list: linked list container

Doubly linked list structure with head node
push_back O (1) push_front O (1) insert (it, val) O (1)
pop_back O (1) pop_front O (1) erase (it) O (1)
iterator traversal O (n )

Add delete list and random access vector

Efficiency related method splice function

list::splice实现list拼接的功能。
将源list的内容部分或全部元素删除(直接拿来 然后直接指针指向)
拼插入到目的list。
函数有以下三种声明:
void splice ( iterator position, list<T,Allocator>& x );  //
void splice ( iterator position, list<T,Allocator>& x, iterator i );
void splice ( iterator position, list<T,Allocator>& x, iterator first, iterator last );
函数说明:在list间移动元素

将x的元素移动到目的list的指定位置,高效的将他们插入到目的list并从x中删除。
目的list的大小会增加,增加的大小为插入元素的大小。x的大小相应的会减少同样的大小。
前两个函数不会涉及到元素的创建或销毁。第三个函数会。
指向被删除元素的迭代器会失效。

empty size front back…

Invalidation problem:
refresh when adding or deleting containers

// 迭代器的失效问题
    for (auto it = vec.begin();
        it != vec.end();
        ++it)
    {
        if (*it > 20)
        {
            it = vec.insert(it, 100);  
//不能只写成vec.insert(it, 100)
            ++it;
        }
    }

Container adapter

1.stack: stack

The bottom layer uses deque to store the elements, and the deque method is called
stack s1;
stack <int, vector> s2; // I have specified that vector is used as the container container
s1.push (10); s1.pop (); s1.top (); s1.empty (); s1.size ()

Why use deque instead of vector by default?

1.初始的时候deque的二维已经有一段空间,而vector
默认没空间.所以初始内存利用率不高 用deque
2.deue的空间不连续,所以插入删除的效率高

2.queue: queue

The bottom layer uses deque to store the elements, and the method of deque is called
queue q1;
queue <int, vector> q1;
q1.push (10); q1.pop (); q1.front (); q1.back (); empty size

为什么默认用deque不用vector?
1.头尾操作方便

3.priority_queue: priority queue

The default implementation of the vector priority queue is a large-heap heap
with priority. Not every time it is output, it is implemented from the beginning (using the highest priority on the top of the heap). Because of the large-heap heap, the largest element in the queue is the largest element. of

priority_queue<int> pq;
priority_queue<int, deque<int>> pq;
pq.push(10);
pq.push(45);
pq.push(31);
pq.push(21);
pq.push(89);
while(!pq.empty())
{
    int top = pq.top();..用top返回
    cout<<top<<endl;   // 89 45 31 21 10
    pq.pop();//只出不返回
}

Why don't you use vector deque by default?
1. To build a heap, the heap node search needs to use the array index to find, so the following table can be calculated on continuous memory. The
priority queue solves the top k problem of big data

The underlying default parameters of the priority queue:

class _Ty,//数据
class _Container = vector<_Ty>,//容器类型
class _Pr = less<typename _Container::value_type> >//函数类型
int main()
{
vector<int> vec;
for (int i = 0; i < 100000; ++i)
{
vec.push_back(rand() % 10000);
}


unordered_map<int, int> map;
for (int val : vec)
{
map[val]++;
}


// function 函数对象类型
// 小堆结构 priority_queue<pair<int, int>, vector<pair<int, int>>, greater>
using P = pair<int, int>;
using FUNC = function<bool (P&,P&)>;
using MinHeap = priority_queue<P, vector<P>, FUNC>;
MinHeap minHeap([](auto &a, auto &b)->bool
{
return a.second > b.second;
});


for_each(map.begin(),
map.end(),
[&minHeap](auto &pair)->void
{
if (minHeap.size() < 5)
{
minHeap.push(pair);
}
else
{
if (pair.second > minHeap.top().second)
{
minHeap.pop();
minHeap.push(pair);
}
}
});


while (!minHeap.empty())
{
cout << "数字:" << minHeap.top().first << " 重复次数:"
<< minHeap.top().second << endl;
minHeap.pop();
}
cout << endl;


return 0;
}

Associated container

Collection key mapping table [key, value] id-> Student

1. Ordered container
red-black tree

红黑树  insert/erase/query:O(log2n)  O(1) > O(log2n) > O(n) > O(n^2)
       有序的
8  12   23   35   47   59   62   69  78  89   95
                       59
            23                      78
     8            35           62           89
        12           47          69             95

2. Unordered container
chained hash table O (1)

In ordinary hash tables, the value of key is exactly the same as the value of value, that is, the data value itself is used as the subscript of the array . This is a direct mapping, such as array [2], array [20], array [35] 36 spaces, directly accessed by key, so the addition, deletion and modification are all O (1), but the space is occupied too much.
Insert picture description here
Hash hash table:
** Hashing technology is in the storage location of the record and his keywords Establish a certain correspondence between f, ** is that each keyword key corresponds to a storage location f (key). When searching, find the mapping f (key) of the given value key according to this corresponding relationship. If there is this record in the search set, it must be in the position of f (key). We call this correspondence f a hash function, also known as a hash function.

The mapping function is called a hash function, and the array that stores the records is called a hash table.
Definition of hash function:

关键字key对应一个存储位置f(key)。查找时,
根据这个对应的关系找到给定值key的映射f(key)
若查找集合中存在这个记录,则必定在f(key)的位置上。
我们把这种对应关系f成为散列函数

The definition of hash table is explained in detail in the next section

Published 82 original articles · praised 7 · visits 4182

Guess you like

Origin blog.csdn.net/sunshine612/article/details/105015837