pair以模板的方式存储两个数据

namespace std {
template <typename T1, typename T2>
struct pair {
// member
T1 first;
T2 second;
...
};
}

p.first
p.second

get<0>(p) C++11
get<1>(p) C++11

示例 PairPrintTest()

//=======================================
pair接受多个参数
示例 PairTuple()
//========================================
make_pair 中使用ref() 代替参数的引用&
示例 PairRefTest
//=============================================
tie 的用法
示例 TieTest
//=====================================

#include <tuple>
#include <iostream>
#include <utility>
#include <tuple>
#include <complex>
#include <string> using namespace std; template<typename T1,typename T2>
ostream& operator << (ostream& strm,
const pair<T1, T2>&p)
{
return strm << "[" << p.first << "," << p.second << "]";
} void PairPrintTest()
{
typedef std::pair<int, float> IntFloatPair;
IntFloatPair p(, 3.14f); cout << get<>(p) << " "<< get<>(p) << endl;
cout << p << endl;
} //==============================
class Foo {
public:
Foo(tuple<int, float>) {
cout << "Foo::Foo(tuple)" << endl;
}
template <typename... Args>
Foo(Args... args) {
cout << "Foo::Foo(args...)" << endl;
}
}; void PairTupleTest()
{
tuple<int, float> t(, 2.22f);
// pass the tuple as a whole to the constructor ofFoo:
pair<int, Foo> p1(, t);
// pass the elements ofthe tuple to the constructor ofFoo:
pair<int, Foo> p2(piecewise_construct, make_tuple(), t); } void PairRefTest()
{
int i = ;
auto p = make_pair(&i, &i);
++p.first;
++p.second;
cout << "When use '&' i: " << i << endl; auto p1 = make_pair(ref(i), ref(i));
++p1.first;
++p1.second;
cout << "When use 'ref()' i: " << i << endl;
} void TieTest()
{
pair<char, char> p = make_pair('x', 'y'); //pair oftwo chars
char c;
tie(ignore, c) = p; //extract second value into c (ignore first one)
cout << c << endl;
tie(c, ignore) = p;
cout << c << endl;
} void TurpleTest()
{
int n = ;
auto tt = std::tuple_cat(std::make_tuple(, 7.7, "hello"),
std::tie(n));
} int _tmain(int argc, _TCHAR* argv[])
{
PairPrintTest();
PairTupleTest();
PairRefTest();
TieTest();
TurpleTest();
return ;
}

c++11 stl 学习之 pair的更多相关文章

  1. c++11 stl 学习之 shared_ptr

    shared_ptr智能指针 shared_ptr 的声明初始化方式由于指针指针使用explicit参数 必须显示声明初始化shared_ptr<string> pNico = new s ...

  2. 侯捷STL学习(11)--算仿+仿函数+适配器

    layout: post title: 侯捷STL学习(十一) date: 2017-07-24 tag: 侯捷STL --- 第三讲 标准库内核分析-算法 标准库算法形式 iterator分类 不同 ...

  3. ###STL学习--关联容器

    点击查看Evernote原文. #@author: gr #@date: 2014-08-23 #@email: forgerui@gmail.com STL中的关联容器. ###stl学习 |--迭 ...

  4. STL学习:STL库vector、string、set、map用法

    本文仅介绍了如何使用它们常用的方法. vector 1.可随机访问,可在尾部插入元素:2.内存自动管理:3.头文件#include <vector> 1.创建vector对象 一维: (1 ...

  5. Effective STL 学习笔记: Item 22 ~ 24

    Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...

  6. map--C++ STL 学习

    map–C++ STL 学习   Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力.   说下map内 ...

  7. 2018面向对象程序设计(Java)第11周学习指导及要求

    2018面向对象程序设计(Java)第11周学习指导及要求 (2018.11.8-2018.11.11)   学习目标 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: ...

  8. Effective STL 学习笔记 32 ~ 33

    Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...

  9. 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)

    layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...

随机推荐

  1. Postman的基础使用

    postman的基础功能,官方文档介绍的是相当啰嗦,所以笔者这里先简单介绍一下主界面,入门功能就都提到了.稍后我们再一一介绍基础功能的使用方法. Collections:在Postman中,Colle ...

  2. redis——队列

    Redis消息通知系统的实现 Posted on 2012-02-29 最近忙着用Redis实现一个消息通知系统,今天大概总结了一下技术细节,其中演示代码如果没有特殊说明,使用的都是PhpRedis扩 ...

  3. NavitForMySql 破解工具使用

    Navicat 11.0注册机使用教程: 1.右键-管理员权限运行注册机2.选择对应的产品3.点击“补丁”按钮,选择文件4.点击“生成”按钮,生成序列号,并保存下授权文件5.复制序列号,打开软件,在弹 ...

  4. CSS镂空图片处理

    来源:http://www.zhangxinxu.com/wordpress/?p=5267,分享收藏 使用镂空图片,通过CSS改变颜色,达到图片切换的效果,可以同过背景图,然后改变背景色,从而达到图 ...

  5. time 时间内置模块3种形态的转化

    import time print(time.time())  #获得时间戳 1526998642.877814 print(time.sleep(2))  #停止2秒 print(time.gmti ...

  6. UI5-文档-2.1-使用OpenUI5开发应用

    使用OpenUI5和您选择的开发环境(编辑器和Web服务器)开发应用程序.您可以下载所有的源代码,也可以参考OpenUI5的在线版本. 下载OpenUI5 下载和安装OpenUI5的默认方式是从htt ...

  7. MySQL的事务处理及隔离级别

      事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序列都能成为事务.一般来说,事务是必须满足4个条件(ACID)       原子性(Autmic):事务在执行性,要 ...

  8. rook 记录

    更改rook 集群的配置 https://github.com/rook/rook/blob/master/design/cluster-update.md rook集群升级流程 https://ro ...

  9. input:file属性

    1.accept 只能选择png和gif图片 <input id="fileId1" type="file" accept="image/png ...

  10. js中的变量提升(hoisting)

    来看如下代码: function HelloJS(){ var array = [1,2,3,4,5]; for(var i in array){ } alert(i); } HelloJS(); a ...