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. django-template-forloop

    forloop.counter0   # 是每次循环的index 红色的div标签,居然可以这样写. ex:第一次循环的结果 <div class="item active" ...

  2. 39. 拼接表字段b.day

    var fun = ABS_LOADBEAN("com.plug.FunctionHelper");//var v_div = fun.funHelper.strAdd(" ...

  3. mime设置

    ie9对mime有特殊要求,必须要有type才可以. 如果出现css的mime类型不支持.则没有加 type="css/text" 查看本机的mime支持: regedit > ...

  4. dmidecode详解

    1.DMI简介 DMI (Desktop Management Interface, DMI)就是帮助收集电脑系统信息的管理系统,DMI信息的收集必须在严格遵照SMBIOS规范的前提下进行. SMBI ...

  5. 分页导航jsp

    <c:choose>标签与Java switch语句的功能一样,用于在众多选项中做出选择. switch语句中有case,而<c:choose>标签中对应有<c:when ...

  6. Filter接口编写过滤器

      Filter,过滤器,顾名思义,即是对数据等的过滤,预处理过程.为什么要引入过滤器呢?在平常访问网站的时候,有时候发一些敏感的信息,发出后显示时 就会将敏感信息用*等字符替代,这就是用过滤器对信息 ...

  7. VS Code常用快捷键总结

    目录: 1.主命令框 2.常用快捷键   (1) 编辑器与窗口管理   (2) 代码编辑     <1> 格式调整     <2> 光标相关     <3> 重构代 ...

  8. fio 测试磁盘

    root@rook-test:/# ceph osd status+----+-----------------------------+-------+-------+--------+------ ...

  9. DESeq2包

    1)简介: DESeq2-package: for differential analysis of count data(对count data 做差异分析) 2)安装 if("DESeq ...

  10. HashCode的理解

    一.hashcode是什么 要理解hashcode首先要理解hash表这个概念 1. 哈希表 hash表也称散列表(Hash table),是根据关键码值(Key value)而直接进行访问的数据结构 ...