当前位置:网站首页>[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

原网站

版权声明
本文为[Shu Yang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206230556565790.html