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. 搭建 yum 仓库

    翻译来自:https://wiki.centos.org/HowTos/CreateLocalRepos 本地仓库 http 仓库 测试 Steps: 1.把rpm包放在一个目录中.可以根据需要在该目 ...

  2. Mysql-表关系

    表关系分为三种:一对一,一对多,多对多 一对多:一个学院对应多个学生,而一个学生只对应一个学院   --  这儿classroom 是代表的学院. -- 一对多 - A表的一条记录 对应 B 表多条记 ...

  3. C++官方文档-this

    #include <iostream> using namespace std; class Dummy { public: int x; Dummy() : x() { } ; Dumm ...

  4. OpenACC 计算构建内的自定义函数

    ▶ 使用 routine 构件创建的自定义函数,在并行调用上的差别 ● 代码,自定义一个 sqab 函数,使用内建函数 fabsf 和 sqrtf 计算一个矩阵所有元素绝对值的平方根 #include ...

  5. Windows系统日常运维

    WINDOWS系统日常运维 http://www.docin.com/p-677263438.html

  6. leetcode743

    class Solution { public: int networkDelayTime(vector<vector<int>>& times, int N, int ...

  7. 可视化库-seaborn-单变量绘图(第五天)

    1. sns.distplot 画直方图 import numpy as np import pandas as pd from scipy import stats, integrate impor ...

  8. 切换svn登录账户

    右键-->TortoiseSVN-->设置 找到:点击已缓存数据 在认证数据一栏点击:清空

  9. C#实现支持单点登录的一个存储用户信息的类

    网上有很多介绍单点登录的文章,但多为架构设计以及概念性文章,而本文将介绍单点登录的具体具体实现 利用哈希表,作为保存登录用户的队列        private static Hashtable m_ ...

  10. 吴裕雄 数据挖掘与分析案例实战(13)——GBDT模型的应用

    # 导入第三方包import pandas as pdimport matplotlib.pyplot as plt # 读入数据default = pd.read_excel(r'F:\\pytho ...