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. !!! jquery mobile常用代码

    Jquery MOBILE:  (2014-7-1 发布jquery.mobile 1.4.3版本) <!doctype html> <html> <head> & ...

  2. (18/24) webpack实战技巧:快速入门webpack模块化配置

    搞个小例子便于学习: 具体操作为把上节中的webpack.config.js中的entry入口文件进行模块化设置,单独拿出来制作成一个模块. 1.在根目录新建一个config文件,然后新建webpac ...

  3. oracle使用随笔

    一,centos 7安装桌面环境 首先使用命令yum grouplist查看可以使用的group ,第一步,命令安装Gnome Desktop,第二步,yum groupinstall "X ...

  4. Delphi XE6打电话

    procedure TPhoneDialerForm.btnMakeCallClick(Sender: TObject); var PhoneDialerService: IFMXPhoneDiale ...

  5. LiveBinding应用 dataBind 数据绑定

    http://blog.csdn.net/embarcaderochina/article/details/50352193 firemonkey grid/listview dataBind,数据绑 ...

  6. php缓存类

    <?php /* * 缓存类 cache * 实 例: include( "cache.php" ); $cache = new cache(30); $cache-> ...

  7. margin-top和padding-top

    padding- top 在原有的基础上进一步的扩张 margin - top 在原有的位置上发生上下的平移 <!DOCTYPE html> <html lang="en& ...

  8. RESTful介绍和使用教程

    出自:https://blog.csdn.net/x541211190/article/details/81141459 一.REST起源REST(Representational State Tra ...

  9. DrawDib 使用例子<转>

    #include<vfw.h>#pragma comment(lib,"Vfw32.lib") BITMAPINFOHEADER biHeader; memset(&a ...

  10. 使用JavaScript的XMLHttpRequest发送POST、GET请求以及接收返回值

    使用XMLHttpRequest对象分为4部完成: 1.创建XMLHttpRequest组建 2.设置回调函数 3.初始化XMLHttpRequest组建 4.发送请求 实例代码: [javascri ...