c++11 stl 学习之 pair
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的更多相关文章
- c++11 stl 学习之 shared_ptr
shared_ptr智能指针 shared_ptr 的声明初始化方式由于指针指针使用explicit参数 必须显示声明初始化shared_ptr<string> pNico = new s ...
- 侯捷STL学习(11)--算仿+仿函数+适配器
layout: post title: 侯捷STL学习(十一) date: 2017-07-24 tag: 侯捷STL --- 第三讲 标准库内核分析-算法 标准库算法形式 iterator分类 不同 ...
- ###STL学习--关联容器
点击查看Evernote原文. #@author: gr #@date: 2014-08-23 #@email: forgerui@gmail.com STL中的关联容器. ###stl学习 |--迭 ...
- STL学习:STL库vector、string、set、map用法
本文仅介绍了如何使用它们常用的方法. vector 1.可随机访问,可在尾部插入元素:2.内存自动管理:3.头文件#include <vector> 1.创建vector对象 一维: (1 ...
- Effective STL 学习笔记: Item 22 ~ 24
Effective STL 学习笔记: Item 22 ~ 24 */--> div.org-src-container { font-size: 85%; font-family: monos ...
- map--C++ STL 学习
map–C++ STL 学习 Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力. 说下map内 ...
- 2018面向对象程序设计(Java)第11周学习指导及要求
2018面向对象程序设计(Java)第11周学习指导及要求 (2018.11.8-2018.11.11) 学习目标 (1) 掌握Vetor.Stack.Hashtable三个类的用途及常用API: ...
- Effective STL 学习笔记 32 ~ 33
Effective STL 学习笔记 32 ~ 33 */--> div.org-src-container { font-size: 85%; font-family: monospace; ...
- 侯捷STL学习(九)--关联式容器(Rb_tree,set,map)
layout: post title: 侯捷STL学习(九) date: 2017-07-21 tag: 侯捷STL --- 第十九节 容器rb_tree Red-Black tree是自平衡二叉搜索 ...
随机推荐
- 关于tcp的三次握手与四次挥手,以及粘包
tcp三次握手: TCP是因特网中的传输层协议,使用三次握手协议建立连接.当主动方发出SYN连接请求后,等待对方回答SYN+ACK[1],并最终对对方的 SYN 执行 ACK 确认.这种建立连接的方法 ...
- shutil模块---文件,文件夹复制、删除、压缩等处理
shutil模块:高级的文件,文件夹,压缩包处理 拷贝内容 # shutil.copyfileobj(open('example.ini','r'),open('example.new','w')) ...
- python入门-WHILE循环
1 使用while循环 current_number= : print(current_number) current_number += 2 让用户选择退出 prompt = "tell ...
- 0_Simple__simpleTexture + 0_Simple__simpleTextureDrv
使用纹理引用来旋转图片,并在使用了静态编译和运行时编译两种环境. ▶ 源代码:静态编译 #include <stdio.h> #include <windows.h> #inc ...
- reportng代替testng的默认报告——pom设置
既然是maven项目,直接关注pom设置. 这篇写得很清楚了:maven+testng+reportng的pom设置 强调一点的是,guice必须依赖,就因这个卡了大半天. <dependenc ...
- xe7 控件升级
rm.ehlib.synedit OK SynSQLSyn1->TableNames 为NULL,导致添加数据失败,XE6正常 放在按钮里也不正常,就不说初始化次序引起的.
- UI5-文档-4.12-Shell Control as Container
现在我们使用shell控件作为应用程序的容器,并使用它作为新的根元素.shell通过在桌面屏幕上引入一个所谓的信箱,来负责应用程序对设备屏幕大小的视觉调整. Preview The app is no ...
- Haskell语言学习笔记(57)Parsec(4)
Parser 类型 data ParsecT s u m a type Parsec s u = ParsecT s u Identity type Parser = Parsec String () ...
- Linux CentOS7中 设置IP地址、网关DNS
cd /etc/sysconfig/network-scripts/ #进入网络配置文件目录 vi ifcfg-eno16777736 #编辑配置文件,此处eno后边的编号因电脑而易 TYPE ...
- 面向对象三大特性一一继承(inheritance)和组合(Composition)
记住2句话(常识),像个正常人思考! 1.“ is-a ”关系 用 继承! 学生是人,学生继承人这个类, 2. “has-a ”关系 用 组合!电脑有显卡,那么我们就在计算机类中增加显卡属性来复用显 ...