当前位置:网站首页>[STL] summary of pair usage
[STL] summary of pair usage
2022-06-23 06:57:00 【Shu Yang】
Learning to associate containers (map、unordered_map……) Before , We need to know the name first pair Standard library type of . One pair Save two data members , The first member is first, The second member is second.
pair What's the use of it ? For example, students have student numbers and names , These two properties are corresponding , At this time, using an array to save student ID and name will not reflect their corresponding relationship , We can use pair To preserve . One pair Save a student's student number and name , Many students have many student numbers and names pair To preserve .
map It's a kind of associative container ,map Is made up of pair form . Suppose there is a bedroom 501, Pictured , The bedroom has 4 A student , We're going to use a container to hold this 4 Student numbers and names of students , We can use one pair Save a student's student number and name , One map To save a bedroom 4 A student ,map The members of 4 individual pair.
usage
| pair<T1,T2> p; | Create a pair, Contains two that have been initialized by default , The types are T1、T2 Members of |
|---|---|
| pair<T1,T2> p(v1,v2); | Create a pair,first The type of member is T1, The value is v1,second The type of member is T2, The value is v2 |
| pair<T1,T2> p={v1,v2}; | Create a pair,first The type of member is T1, The value is v1,second The type of member is T2, The value is v2 |
| make_pair(v1,v2); | Return a message with v1 and v2 The initialization of the pair,pair Type from v1 and v2 Infer the type of |
| p.first; | return p Of first Data member |
| p.second; | return p Of second Data member |
Program example :
pair<string, string> p1("sc0301"," Xiao Yang "); // Mode one , Create a pair be known as p1
pair<string, string> p2 = make_pair("sc0302", " The pony "); // Mode two ,make_pair Function returns a function with "sc0302" and " The pony " The initialization of the pair
pair<string, string> p3("sc0303", " Xiao Wang ");
pair<string, string> p4("sc0304", " Xiao He ");
string p1_ID = p1.first; // obtain pair Of the 1 Members
string p1_name = p1.second; // obtain pair Of the 2 Members
pair and map Use it together
It's still the front bedroom 4 Examples of students .
Program example :
#include <iostream>
#include <string>
#include <utility>
#include <map>
using namespace std;
int main() {
pair<string, string> p1("sc0301"," Xiao Yang "); // Mode one , Create a pair be known as p1
pair<string, string> p2 = make_pair("sc0302", " The pony "); // Mode two ,make_pair Function returns a function with "sc0302" and " The pony " The initialization of the pair
pair<string, string> p3("sc0303", " Xiao Wang ");
pair<string, string> p4("sc0304", " Xiao He ");
map<string, string> m1; // Create an empty map
map<string, string> m2{
p1,p2,p3,p4 }; // Create a containing pair p1、p2、p3、p4 Of map
map<string, string> m3{
{
"sc0301"," Xiao Yang "},{
"sc0302", " The pony "},{
"sc0303", " Xiao Wang "},{
"sc0304", " Xiao He "} }; // The effect is the same as the previous sentence
map<string, string>::iterator it1 = m2.begin(); // Get a point to m2 Iterator of the first element
map<string, string>::iterator it2 = m2.end(); // Get a point to m2 Iterator for the next position of the tail element
pair<string, string> p11 = *it1; // obtain m2 First element of {"sc0301"," Xiao Yang "}, This is a pair
string p1_ID = it1->first; // obtain m2 First element of {"sc0301"," Xiao Yang "} Of fisrt member , Student number
string p1_name = it1->second; // obtain m2 First element of {"sc0301"," Xiao Yang "} Of second member , full name
for (auto p : m2) {
cout << " Student number :" << p.first << "; full name :" << p.second << endl;
}
m1.insert(p1); // stay map Insert existing pair
m1.insert({
"sc0302", " The pony " }); // Insert key-value pairs { "sc0302", " The pony " }
m1.insert(pair<string, string> ("sc0303", " Xiao Wang ")); // Create an anonymous pair object , And insert into map in
m1.emplace(p1); // The keyword to insert is already in the container ,emplace/insert Don't do anything?
m1.emplace(pair<string, string>("sc0303", " Xiao Wang ")); // The keyword to insert is already in the container ,emplace/insert Don't do anything?
map<string, string>::iterator it = m2.find("sc0301"); // Search keywords are "sc0301" The elements of , Returns an iterator
if (it == m2.end()) {
// if "sc0301" Not in the container , be it Equals the trailing iterator
cout << " Not found !" << endl;
}
else {
pair<string, string> result1 = *it; // eureka
}
int result2 = m2.count("sc0305"); // Search keywords are "sc0301" The elements of , The return keyword is equal to "sc0301" The number of elements
if (result2==0) {
cout << " Not found !" << endl;
}
else {
cout << " eureka !" << endl;
}
}
map Refer to :【C++】STL Associated container map Usage Summary
边栏推荐
猜你喜欢

How to realize video call and live interaction in a small program when live broadcasting is so popular?

redux Actions may not have an undefined “type“ property. Have you misspelled a constant?

Media industry under the epidemic situation, small program ecology driven digital transformation exploration

中台库存中的实仓与虚仓的业务逻辑设计

Children's programming for comprehensively cultivating students' mental thinking

网页制作存在的一些难点

Swagger3 integrates oauth2 authentication token

idea的去除转义的复制粘贴

Badly placed ()‘s 问题

【日常训练】513. 找树左下角的值
随机推荐
QT method of compiling projects using multithreading
数据在内存中的存储方式(C语言)
网页制作存在的一些难点
【Qt】基础学习笔记
Easy EDA #学习笔记09# | ESP32-WROOM-32E模组ESP32-DevKitC-V4开发板 一键下载电路
core. What is JS ---kalrry
ssm + ftp +ueditor
2022年养老理财产品有哪些?风险小的
疫情下的传媒产业,小程序生态驱动数字化转型探索
leetcode - 572. A subtree of another tree
Badly placed()'s problem
Topic35——34. Find the first and last positions of elements in a sorted array
Qt 中 QVariant 使用总结
[graduation season · advanced technology Er] it's my choice. I have to walk on my knees
利用adb 调试设备串口信息的一个小方法
20220620 uniformly completely observable (UCO)
mysql 基础查询
C# DPI适配问题
What are the pension financial products in 2022? Low risk
Xxl-sso enables SSO single sign on