当前位置:网站首页>STL notes (XIII): relevance container - mapping
STL notes (XIII): relevance container - mapping
2022-07-21 14:10:00 【Reyn Morales】
STL note ( 13、 ... and ): Affinity container —— mapping
function
#include <iostream>
#include <map>
using namespace std;
// mapping map( one-on-one )、multimap( One to many )
// internal structure : from pair(key, value) form , With key Constructed red black tree
// iterator : Bidirectional iterator - Can only increase by itself ( reduce ), Cannot add ( reduce ), Use advance iteration
template <typename T>
void Display(T m)
{
for (typename T::iterator it = m.begin(); it != m.end(); it++) {
cout << it->first << ' ' << it->second << endl;
}
cout << endl;
}
// Construction and initialization
void test1()
{
map<int, string> m1; // < key
map<int, string, greater<int> > m2; // >
pair<int, string> p[] = {
make_pair(2, "c"),
make_pair(1, "b"),
make_pair(1, "a")
};
map<int, string> m3(p, p + 3); // 1. key No repetition 2. With key Sort primary keys
Display(m3);
multimap<int, string> m4(p, p + 3); // 1. key Allow repetition 2. With key Sort primary keys
Display(m4);
}
// Access data
void test2()
{
pair<int, int> p[] = {
make_pair(2, 20),
make_pair(1, 10)
};
map<int, int> m(p, p + 2);
Display(m);
// Take the data
// 1. iterator
map<int, int>::iterator it = m.begin(); // (1, 10)
cout << it->first << ' ' << it->second << endl;
// it->first = 3; // key read-only , Do not modify the
it->second = 30; // value You can read , Can also write
cout << it->first << ' ' << it->second << endl;
// 2. [] -> m[key] = value | <multimap The container does not have this method > | When key When there is no ,value Save by default map in
cout << m[2] << endl; // Equivalent to :cout << m.at(2) << endl;
cout << m[3] << endl;
// Save the data
}
// Insert deletion
void test3()
{
map<int, string> m;
// Insert
// 1. Object mode ( The return type is similar to set - pair<iterator, bool>)
pair<int, string> p(2, "Reyn");
m.insert(p);
m.insert(pair<int, string>(1, "Lisa"));
m.insert(make_pair(3, "Jhon"));
// 2. value_type
m.insert(map<int, string>::value_type(4, "Echo"));
// 3. [key] = value
m[4] = "Lily"; // If key There is , The modified value
m[5] = "Echo"; // If key non-existent , Then insert it into the container
Display(m);
// Delete
m.erase(1); // Delete with key by (1) The key/value pair
m.erase(m.begin()); // Bidirectional iterator advance(it, steps)
m.erase(m.begin(), m.end());
}
// Other operating
void test4()
{
multimap<int, int> m;
m.insert(make_pair(1, 10));
m.insert(make_pair(2, 20));
m.insert(make_pair(1, 18));
m.insert(make_pair(0, 8));
m.insert(make_pair(1, 15));
Display(m);
// Count
cout << m.count(1) << endl;
// lookup : If you're looking for key non-existent , return end()
cout << m.find(1)->first << ' ' << m.find(1)->second << endl;
// lower_bound: first key A value greater than or equal to (1) The iterator of the elements of
m.lower_bound(1); // {1:10}
// upper_bound: first key Greater than (1) The iterator of the elements of
m.upper_bound(1); // {2:20}
// equal_range
pair<multimap<int, int>::iterator, multimap<int, int>::iterator> p;
p = m.equal_range(1);
cout << p.first->first << ' ' << p.first->second << endl; // {1:10}
cout << p.second->first << ' ' << p.second->second << endl; // {2:20}
}
int main()
{
test1(); // Construction and initialization
test2(); // Access data
test3(); // Insert deletion
test4(); // Other operating
return 0;
}
Case study
#include <iostream>
#include <string>
#include <map>
using namespace std;
// Query the information of enterprise personnel by Department
/* essential information
The personnel department Reyn 20
The sales department Lily 21
The personnel department Lisa 20
The sales department Jhon 24
The sales department Echo 19
*/
/* Output
The sales department :
Lily 21
Jhon 24
Echo 19
*/
class Person
{
private:
string name;
int age;
public:
Person(string n, int a):name(n), age(a) {}
void showInfo()
{
cout << name << '\t' << age << endl;
}
};
class Manage
{
private:
multimap<string, Person> m; // Consideration should be given to : Judge key Is it possible to compare , Overload operators if necessary , Or use greater
public:
Manage(pair<string, Person> * start, pair<string, Person> * last):m(start, last) {}
void displayMembers()
{
cout << " department \t full name \t Age " << endl;
for (multimap<string, Person>::iterator it = m.begin(); it != m.end(); advance(it, 1)) {
cout << it->first << '\t';
it->second.showInfo();
}
}
void select(string part)
{
// 1. Traversal container
// 2. equal_range
cout << "\"equal_range\"" << endl;
pair<multimap<string, Person>::iterator, multimap<string, Person>::iterator> p = m.equal_range(part);
cout << "===" << part << "===" << endl;
for (multimap<string, Person>::iterator ptr = p.first; ptr != p.second; ptr++) {
ptr->second.showInfo();
}
// 3. find + count
cout << endl << "\"find + count\"" << endl;
multimap<string, Person>::iterator it = m.find(part);
int num = m.count(part);
cout << "===" << part << "===" << endl;
for (int i = 0; i < num; i++, it++) {
it->second.showInfo();
}
}
};
int main()
{
Person p1("Reyn", 20), p2("Lily", 21), p3("Lisa", 20), p4("Jhon", 24), p5("Echo", 19);
pair<string, Person> p[] = {
make_pair(" The personnel department ", p1),
make_pair(" The sales department ", p2),
make_pair(" The personnel department ", p3),
make_pair(" The sales department ", p4),
make_pair(" The sales department ", p5)
};
Manage mng(p, p + 5);
mng.displayMembers();
cout << endl;
mng.select(" The sales department ");
return 0;
}
summary
- map、multimap Is another kind of relational container , Its internal structure is made up of pair(key, value) form , With key Constructed red black tree , Its iterator is also a bidirectional iterator , That is, it can only increase by itself ( reduce ), Cannot add ( reduce ), Use advance iteration
- map、multimap Another feature of is that you can use [] Access and add container elements in the form of , stay [] The key in is the key in the key value pair , besides map、multimap And set、multiset Basically similar
- Another thing to note is ,map And set Medium insert Function has a return value , The type is pair(iterator, bool), Iterators representing the inserted values respectively , And whether the insertion is successful , besides ,find Functions also have return values , Its type is the iterator of the value to be found , If not found , The iterator is returned end()
Containers | type | data structure | iterator |
---|---|---|---|
vector | Sequential container | The dynamic array ( Single port ) | Random access type |
deque | Sequential container | Array ( Double port ) | Random access type |
list | Sequential container | Double linked list | Two way iterative |
stack | Container adapter | vector\deque\list | nothing |
queue | Container adapter | deque\list | nothing |
priority_queue | Container adapter | vector\deque | nothing |
bitset | Binary container | 0 or 1 | nothing |
set\multiset | Affinity container | Red and black trees | Two way iterative |
map\multimap | Affinity container | from pair The composition of the red black tree | Two way iterative |
In the next article , We will introduce C++ Function objects in
Postscript
This article is mainly used as a summary of the course learning process , If you have any questions, you can also leave a message in the comment area , I will try my best to help you solve the problem , If it's helpful to you , I hope you like it more 、 forward , thank you !
边栏推荐
- MySQL最详DDL和DML操作
- 测试计划应包括的内容
- 【程序的编译(预处理操作)+链接】
- ACL和NAT
- IO input / output stream
- C语言中长度为零的数组详解 (2)
- ThreadLocal父子线程传递问题——熟悉阿里的TransmittableThreadLocal
- win11微软账户登录一直转圈怎么解决?win11微软账户登录一直转圈
- Activity registration | expose the insider of Apache Doris data Lake analysis technology? Rare earth Developers Conference free registration!
- IIC communication protocol
猜你喜欢
The White House is trying to promote the filling of hundreds of thousands of cyber security jobs in the United States
认识和使用日志工具Loki
5 种常见的 async/await 误用
Ireport export PDF font bold failure
Basic understanding of ES6
Study notes of pytorch deep learning practice: cyclic neural network (Advanced)
低代码平台搭建跨部门沟通系统案例分析
The computer suddenly shows that there is only C disk, and other disks do not show ---- solution (very simple)
关于 MySQL 的酸与 MVCC 和面试官小战三十回合
【软件测试】测试中的风险有哪些?
随机推荐
npm Warn config global `--global`, `--local` are deprecated. Use `--location=global` instead
低代码平台搭建跨部门沟通系统案例分析
模糊查询 占位符
How can the clothing industry make the real economy smart?
TF 坐标变换
【安装PG】
什么?多商户系统不适配 APP?这不就来了么!
How to build a knowledge base web page?
How can technologists start their personal brand? Exclusive teaching of top five KOLs
C语言中长度为零的数组详解 (2)
STL 笔记(十三):关联性容器——映射
今天测试告诉我数据同步后,合并数据无效
_聚合函数
ThreadLocal parent-child thread delivery problem -- familiar with Ali's transmittablethreadlocal
MySQL---three 多表查询与事务的操作
win11微软账户登录一直转圈怎么解决?win11微软账户登录一直转圈
IO input / output stream
内网渗透学习(一)内网入门基础
一张光盘和一张软盘
助力企业数字化升级,火山引擎发布云上增长解决方案