std::pair是一个结构模板,提供了一种将两个异构对象存储为一个单元的方法.

定义于头文件 <utility>

template<
class T1,
class T2
> struct pair;

  

成员类型 Definition   成员对象 Type
first_type T1         First T1
second_type T2   Second T2
1.定义(构造):

     pair<int, double> p1;          //使用默认构造函数
pair<int, double> p2(1, 2.4); //用给定值初始化
pair<int, double> p3(p2); //拷贝构造函数
2.访问两个元素(通过first和second): pair<int, double> p1; //使用默认构造函数
p1.first = 1;
p1.second = 2.5;
cout << p1.first << " " << p1.second << endl;

  

std::make_pair  创建一个std::pair对象,推导出目标类型的参数类型.

定义于头文件 <utility>

template< class T1, class T2 >
std::pair<T1,T2> make_pair( T1 t, T2 u );
template< class T1, class T2 >
std::pair<V1,V2> make_pair( T1&& t, T2&& u );

  示例:

#include <iostream>
#include <utility>
#include <functional> int main()
{
int n = 1;
int a[5] = {1,2,3,4,5}; // build a pair from two ints
auto p1 = std::make_pair(n, a[1]);
std::cout << "The value of p1 is "
<< "(" << p1.first << ", " << p1.second << ")\n"; // build a pair from a reference to int and an array (decayed to pointer)
auto p2 = std::make_pair(std::ref(n), a);
n = 7;
std::cout << "The value of p2 is "
<< "(" << p2.first << ", " << *(p2.second+1) << ")\n";
}
//The value of p1 is (1, 2)
//The value of p2 is (7, 2)

  

pair与make_pair的示例

#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 string
product2.second = 0.99; // type of second is double product3 = 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;
} //The price of tomatoes is $3.25
//The price of lightbulbs is $0.99
//The price of shoes is $20

  

STL之pair及其非成员函数make_pair()的更多相关文章

  1. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

  2. effective c++:引用传递与值传递,成员函数与非成员函数

    以pass-by-reference-to-const 替换pass-by-value 考虑以下class继承体系 class Person { public: Person(); // parame ...

  3. 读书笔记_Effective_C++_条款四十六:需要类型转换时请为模板定义非成员函数

    这个条款可以看成是条款24的续集,我们先简单回顾一下条款24,它说了为什么类似于operator *这样的重载运算符要定义成非成员函数(是为了保证混合乘法2*SomeRational或者SomeRat ...

  4. C++ 匿名名字空间及静态非成员函数

    在C++中,static有一个感觉被较少提及的用法:修饰非成员函数,这个用法实际是从C语言继承来的.其作用是表明这个函数只在当前编译单元中有效.这就使这个函数的所有引用在编译时就可以全部确定,无需进入 ...

  5. 读书笔记 effective c++ Item 24 如果函数的所有参数都需要类型转换,将其声明成非成员函数

    1. 将需要隐式类型转换的函数声明为成员函数会出现问题 使类支持隐式转换是一个坏的想法.当然也有例外的情况,最常见的一个例子就是数值类型.举个例子,如果你设计一个表示有理数的类,允许从整型到有理数的隐 ...

  6. 读书笔记 effective c++ Item 46 如果想进行类型转换,在模板内部定义非成员函数

    1. 问题的引入——将operator*模板化 Item 24中解释了为什么对于所有参数的隐式类型转换,只有非成员函数是合格的,并且使用了一个为Rational 类创建的operator*函数作为实例 ...

  7. STL之pair对组

    #include<iostream> #include<algorithm> #include<cstring> #include<cstdlib> u ...

  8. STL之rb_tree的find函数

    1 通用的search方法 STL在实现对特定key值的查找时,并没有採用通用的方法: BRTreeNode * rb_tree_search(RBTreeNode * x, int key){ wh ...

  9. [C++面向对象]-C++成员函数和非成员函数

    大纲: 1.成员函数和非成员函数 2.详细解释 3.总结 4.参考   1.成员函数和非成员函数   其实简单来说成员函数是在类中定义的函数,而非成员函数就是普通函数,即不在类中定义的函数,其中非成员 ...

随机推荐

  1. ES6——Promise

    异步和同步 异步,操作之间没有关系,同时执行多个操作, 代码复杂 同步,同时只能做一件事,代码简单 Promise 对象 用同步的方式来书写异步代码 Promise 让异步操作写起来,像在写同步操作的 ...

  2. smarty之缓存机制

    当smarty开启缓存时,当tpl文件内容改变时,则缓存页面会重新生成 test.php: <?php date_default_timezone_set('asia/shanghai'); h ...

  3. EF core 实现读写分离解决方案

    我们公司2019年web开发已迁移至.NET core,目前有部分平台随着用户量增加,单一数据库部署已经无法满足我们的业务需求,一直在寻找EF CORE读写分离解决方案,目前在各大技术论坛上还没找到很 ...

  4. javaweb各种框架组合案例(八):springboot+mybatis-plus+restful

    一.介绍 1. springboot是spring项目的总结+整合 当我们搭smm,ssh,ssjdbc等组合框架时,各种配置不胜其烦,不仅是配置问题,在添加各种依赖时也是让人头疼,关键有些jar包之 ...

  5. Linux下安装Dubbox

    1.Dubbox简介 Dubbox 是一个分布式服务框架,其前身是阿里巴巴开源项目Dubbo ,被国内电商及互联网项目中使用,后期阿里巴巴停止了该项目的维护,当当网便在Dubbo基础上进行优化,并继续 ...

  6. 【Database】Mysql分布式集群学习笔记

    一.sql 的基本操作 (2018年11月29日,笔记) (1)数据库相关操作 创建数据库.查看数据库.删除数据库 #. 创建数据库 create database mytest default ch ...

  7. [python 学习] sys模块

    sys.stdout sys.stdin

  8. 【串线篇】spring boot对静态资源的映射规则

    WebMvcAutoConfiguration的内部类 WebMvcAutoConfigurationAdapter 其中ResourceProperties点进去 其中addResourceHand ...

  9. $mona$要成为高端玩家

    \(mona\)要成为高端玩家! 好在撑过了联赛,接下来要向高端玩家冲击啦! 新时期当然要有新的学习规划啦! 最近的更新(有什么就在这里说啦) 随便更更. \(FFT\)刷着打算先看看生成函数. 感觉 ...

  10. 触发写Redo&nbsp;Log的条件

    参见:http://www.ixora.com.au/notes/redo_write_triggers.htm http://www.eygle.com/archives/2005/02/redoa ...