日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

C++文档阅读笔记-STL中pair的初步解析

發布時間:2025/3/15 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++文档阅读笔记-STL中pair的初步解析 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

一個結構體,這個結構體提供了融合2個對象變為一個對象的能力;

template<class Type1, class Type2>struct pair {typedef Type1 first_type;typedef Type2 second_typeType1 first;Type2 second;pair( );pair(const Type1& __Val1, const Type2& __Val2);template<class Other1, class Other2>pair(const pair<Other1, Other2>& _Right);void swap(pair<Type1, Type2>& _Right)};

下面給出官方的栗子:

運行截圖如下:

源碼如下:

#include <utility> #include <map> #include <iomanip> #include <iostream>int main( ) {using namespace std;// Using the constructor to declare and initialize a pairpair <int, double> p1 ( 10, 1.1e-2 );// Compare using the helper function to declare and initialize a pairpair <int, double> p2;p2 = make_pair ( 10, 2.22e-1 );// Making a copy of a pairpair <int, double> p3 ( p1 );cout.precision ( 3 );cout << "The pair p1 is: ( " << p1.first << ", " << p1.second << " )." << endl;cout << "The pair p2 is: ( " << p2.first << ", " << p2.second << " )." << endl;cout << "The pair p3 is: ( " << p3.first << ", " << p3.second << " )." << endl;// Using a pair for a map elementmap <int, int> m1;map <int, int>::iterator m1_Iter;typedef pair <int, int> Map_Int_Pair;m1.insert ( Map_Int_Pair ( 1, 10 ) );m1.insert ( Map_Int_Pair ( 2, 20 ) );m1.insert ( Map_Int_Pair ( 3, 30 ) );cout << "The element pairs of the map m1 are:";for ( m1_Iter = m1.begin( ); m1_Iter != m1.end( ); m1_Iter++ )cout << " ( " << m1_Iter -> first << ", "<< m1_Iter -> second << " )";cout << "." << endl;// Using pair as a return type for a functionpair< map<int,int>::iterator, bool > pr1, pr2;pr1 = m1.insert ( Map_Int_Pair ( 4, 40 ) );pr2 = m1.insert ( Map_Int_Pair (1, 10 ) );if( pr1.second == true ){cout << "The element (4,40) was inserted successfully in m1."<< endl;}else {cout << "The element with a key value of\n"<< " ( (pr1.first) -> first ) = " << ( pr1.first ) -> first << " is already in m1,\n so the insertion failed." << endl;}if( pr2.second == true ){cout << "The element (1,10) was inserted successfully in m1."<< endl;}else {cout << "The element with a key value of\n"<< " ( (pr2.first) -> first ) = " << ( pr2.first ) -> first << " is already in m1,\n so the insertion failed." << endl;}getchar(); }

從這個例子可以看出,pair一般與map搭配使用,難怪看大佬敲代碼,就會用pair進行操作!

?

總結

以上是生活随笔為你收集整理的C++文档阅读笔记-STL中pair的初步解析的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。