[6] NDK Series C ++ containers

container

Container is used for storing things in the box.

Common container comprising: an array array, linked list list, tree, tree, stack, stack, queue queue, hash hash table, collection set, mapping tables map the like. The container is accommodating these data structures. The data structure is divided into two kinds of sequences associated with the formula, the container is also divided into a sequence container and associated containers .

STL Standard Template Library, the core including containers, algorithms, iterators.

Element arrangement order irrespective of the element, the element is added to the container by the order determination

Sequence container / vessel order

container Explanation
vector It supports fast random access
list Support to quickly insert, delete
and Deque ends are allowed to enqueue and dequeue a queue
stack LIFO LIFO (Last In First Out) stack
queue FIFO FIFO (First Input First Output) queue
priority_queue There's queue priority management

Vector (vector)

Continuous storage elements

Iterator

//获得指向首元素的迭代器  模板类,不是指针,当做指针来使用
vector<int>::iterator it = vec.begin();
//遍历元素
for (; it < vec.end(); it++)
{
	cout << *it << endl;
}
//begin和end   分别获得 指向容器第一个元素和最后一个元素下一个位置的迭代器
//rbegin和rend 分别获得 指向容器最后一个元素和第一个元素前一个位置的迭代器

//注意循环中操作元素对迭代器的影响
vector<int>::iterator it = vec.begin();
for (; it < vec.end(); )
{
    //删除值为2的元素 
	if (*it == 2) {
		vec.erase(it);
	}
	else {
		it++;
	}
}

List (list)

Doubly linked list of nodes, each node contains an element

Deque (the deque)

An array of pointers point to different elements stored in consecutive consisting of

Stack (stack)

Arrangement LIFO value

stack<int> s;
//入栈
s.push(1);
s.push(2);
//弹栈
s.pop();
//栈顶
cout << s.top() << endl;

Queue (queue)

FIFO arrangement value

queue<int> q;
q.push(1);
q.push(2);
//移除最后一个
q.pop();
//获得第一个
q.front();
//最后一个元素
cout << q.back() << endl;

Priority queue (The priority_queue)

The order of elements arranged in a queue by a certain value of the stored data

//最大的在队首
priority_queue<int>;
//在vector之上实现的
priority_queue<int, vector<int>, less<int> >; 
//vector 承载底层数据结构堆的容器
//less 表示数字大的优先级高,而 greater 表示数字小的优先级高
//less  	 让优先队列总是把最大的元素放在队首
//greater    让优先队列总是把最小的元素放在队首

//less和greater都是一个模板结构体 也可以自定义

class Student {
public:
	int grade;
	Student(int grade):grade(grade) {
	}
};
struct cmp {
	bool operator ()(Student* s1, Student* s2) {
        // > 从小到大
        // < 从大到小 
		return s1->grade > s2->grade;
	}
	bool operator ()(Student s1, Student s2) {
		return s1.grade > s2.grade;
	}
};
priority_queue<Student*, vector<Student*>, cmp > q1;
q1.push(new Student(2));
q1.push(new Student(1));
q1.push(new Student(3));
cout << q1.top()->grade << endl;

Associative containers

Most consistent sequence associated container and container operations

The associated container elements are by key to save and access support efficient keyword search and access

Collection (set)

Red-black tree consists of nodes, each node contains an element, the element can not be repeated

set<string> a;  
set<string> a1={"fengxin","666"};
a.insert("fengxin");  // 插入一个元素
a.erase("123");	//删除

Key-value pairs (map)

{Set by the key, value} pair consisting of

map<int, string> m;
map<int, string> m1 = { { 1,"dds" },{ 2,"dds" } };
//插入元素
m1.insert({ 3,"ddss" });
//pair=键值对
pair<int, string> p(4, "ddsd");
m1.insert(p);
//insetrt 返回 map<int, string>::iterator : bool 键值对
//如果 插入已经存在的 key,则会插入失败   
//multimap:允许重复key
//使用m1[3] = "xx" 能够覆盖


//通过【key】操作元素
m1[5] = "yihan";
cout << m1[5].c_str() << endl; 
//通过key查找元素
map<int, string>::iterator it = m1.find(3);
cout << (*it).second.c_str()<< endl;
// 删除 
m1.erase(5);
//遍历
for (it = m1.begin(); it != m1.end(); it++)
{
	pair<int, string> item = *it;
	cout << item.first << ":" << item.second.c_str() << endl;
}


unordered_map

c ++ 11 hash_map substituted (hash table implementations, disordered)

Hash table lookup speed achieved will be achieved faster than RB tree, but overall rb save more memory

Need unordered container, high-frequency quickly find deleted data with a large amount of unordered_map;

Requiring ordered container, look for deletion frequency stability, use care when memory map.

Red-black tree

Red-black tree (Red Black Tree), also known as RB tree, is a relatively balanced binary tree.

1. The node is red or black.

2. The root is black.

3. Each leaf node (blank nodes) are black.

Two children 4 each red node are black. (Red can not have two consecutive nodes on all paths from the root to each leaf)

5. from any node to all leaves of each path contains the same number of black nodes.

1, insert 7

  1. Insert a new node is always red node
  2. If the parent node of the insertion node is black, can maintain properties
  3. If you insert a node's parent is red, destroyed property. Insertion algorithm is re-colored by the rotation or to maintain the properties

After inserting 7, undermining the rule, then the need for different strategies depending on the situation so that balance and meet the rules.

7 of 8 parent node 12 and uncle are red, then we can be 8, 12, and two black redraw redraw grandparent 9 red.

Here 9 is the root node, in order to satisfy Rule 1, took it to redraw the black.

After adjustment:

1, insert 7

Now meet the five rules, so 7 is inserted to complete.

Then insert 6

Insert 6

The new parent node 6 is now left node 7, and node 6 missing uncle, parent node 7 is a left child node grandfather 8,

In this case, we conduct a grandfather right rotation for node node 6 of 8

Right rotation:

Two red-black tree nodes rotating clockwise, so that the parent node is replaced left their children, and their children become their own right.

Left rotation is reversed

6 right-handed insertion

7 and 8 and then switch the color of

6 is rotated to switch the color insert

5,5 and 6 are then inserted into the red, the parent node 6 and node 8 uncle plotted as black, red grandfather to 7, the final

The final insert 5

Guess you like

Origin blog.csdn.net/u011077027/article/details/93749069