[C++]STL pair
来源:互联网 发布:java解析pdf文件内容 编辑:程序博客网 时间:2024/05/18 16:18
本文转自http://blog.csdn.net/wangran51/article/details/8853565
Pair类型概述
pair是一种模板类型,其中包含两个数据值,两个数据的类型可以不同,基本的定义如下:
pair<int, string> a;
表示a中有两个类型,第一个元素是int型的,第二个元素是string类型的,如果创建pair的时候没有对其进行初始化,则调用默认构造函数对其初始化。
pair<string, string> a("James", "Joy");
也可以像上面一样在定义的时候直接对其初始化。
由于pair类型的使用比较繁琐,因为如果要定义多个形同的pair类型的时候,可以时候typedef简化声明:
typedef pair<string, string> author;
author pro("May", "Lily");
author joye("James", "Joyce");
Pair对象的操作
- 对于pair类,由于它只有两个元素,分别名为first和second,因此直接使用普通的点操作符即可访问其成员
pair<string, string> a("Lily", "Poly");
string name;
name = pair.second;
- 生成新的pair对象
可以使用make_pair对已存在的两个数据构造一个新的pair类型:
int a = 8;
string m = "James";
pair<int, string> newone;
newone = make_pair(a, m);
#include <iostream>#include <utility>#include <string>using namespace std;int main () {pair <string,double> product1 ("tomatoes",3.25);pair <string,double> product2;pair <string,double> product3;product2.first ="lightbulbs"; // type of first is stringproduct2.second =0.99; // type of second is doubleproduct3 = make_pair ("shoes",20.0);cout <<"The price of "<< product1.first <<" is $"<< product1.second <<"\n";cout <<"The price of "<< product2.first <<" is $"<< product2.second <<"\n";cout <<"The price of "<< product3.first <<" is $"<< product3.second <<"\n";return 0;}
其运行结果如下:
1The price of tomatoes is $3.25
2The price of lightbulbs is $0.99
3The price of shoes is $20
0 0
- 【C/C++】STL--pair
- C++STL学习Pair
- [C++]STL pair
- c++STL pair的基本用法
- STL Pair
- pair(STL)
- [STL] pair
- STL::pair
- STL-pair
- stl pair
- STL pair
- stl-map stl-pair
- 【STL】STL之pair
- C++STL中的tuple 与 pair (piecewise construction)
- C++ STL Pair
- C++ STL Pair
- stl pair 应用
- poj2002 STL pair解法
- Github最火开源项目-LitePal数据库使用
- Android开发环境离线搭建
- vector内存机制和性能分析
- [Leetcode]_24 Swap Nodes in Pairs
- ToolBar实现滑动伸缩效果
- [C++]STL pair
- spoj Longest Common Substring II
- Google Chrome离线下载地址
- 从上到下打印二叉树
- 很容易输入不期望的输入
- 算法导论——24.1 BellmanFord算法java实现
- Java8特性系列文章目录
- String、StringBuffer、与StringBuilder的区别
- vector动态二维数组(容器的容器)占用内存分析