C++11--Tuple类<tuple>
#include "stdafx.h"
#include <iomanip>
#include <condition_variable>
#include <deque>
#include <functional>
#include <iostream>
#include <fstream>
#include <thread>
#include <map>
#include <unordered_map>
#include <string>
#include <mutex>
#include <algorithm>
#include <sstream>
#include <future>
#include <tuple>
#include <random>
using namespace std;
struct Node {
char id;
int value;
Node(char i, int v) : id(i), value(v) {}
Node() : id(0), value('z') {}
};
int main() {
tuple<int, string, char> t(32, "Penny wise", 'a');
tuple<int, string, char> t = {32, "Penny wise", 'a'}; // 编译不过,构造函数是explicit的
cout << get<0>(t) << endl;
cout << get<1>(t) << endl;
cout << get<2>(t) << endl;
get<1>(t) = "Pound foolish";
cout << get<1>(t) << endl;
string& s = get<1>(t);
s = "Patience is virtue";
cout << get<1>(t) << endl;
//get<3>(t); // 编译不过,t只有3个字段
// get<1>(t) 类似于vector中的t[1]
int i = 1;
//get<i>(t); // 编译不过,i必须是编译时常数
tuple<int, string, char> t2; // 默认构造
t2 = tuple<int, string, char>(12, "Curiosity kills the cat", 'd');
t2 = make_tuple(12, "Curiosity kills the cat", 'd');
if (t > t2) { // 词典比较
cout << "t is larger than t2" << endl;
}
t = t2; // 逐成员拷贝
// Tuple可以储存引用!! 诸如vector这样的STL容器不能。 Pair也可以
string st = "In for a penny";
tuple<string&> t3(st);
//auto t3 = make_tuple(ref(st)); // 同上
get<0>(t3) = "In for a pound"; // st has "In for a pound"
cout << st << endl;
t2 = make_tuple(12, "Curiosity kills the cat", 'd');
int x;
string y;
char z;
std::make_tuple(std::ref(x), std::ref(y), std::ref(z)) = t2; // 将t2赋值给to x, y, z
std::tie(x,y,z) = t2; // 同上
std::tie(x, std::ignore, z) = t2; // 好处是tie可以有选择,get<1>(t2) is ignored
// 其他特性
auto t4 = std::tuple_cat( t2, t3 ); // t4是t2和t3级联之后的结果tuple<int, string, char, string>
cout << get<3>(t4) << endl; // "In for a pound"
// 类型特征 type traits
cout << std::tuple_size<decltype(t4)>::value << endl; // Output: 4
std::tuple_element<1, decltype(t4)>::type dd; // dd是string类型
}
// tuple vs struct
tuple<string, int> getNameAge() {
return make_tuple("Bob", 34);
}
int main() {
struct Person { string name; int age; } p;
tuple<string, int> t;
cout << p.name << " " << p.age << endl; //用struct有利于代码review
cout << get<0>(t) << " " << get<1>(t) << endl; //tuple方便
// 作为单次使用的结构来传输一组数据
string name;
int age;
tie(name, age) = getNameAge();
// 比较tuples
tuple<int, int, int> time1, time2; // hours, minutes, seconds
if (time1 > time2)
cout << " time1 is a later time";
// 多索引的map
map<tuple<int,int,int>, string> timemap;
timemap.insert(make_pair(make_tuple(12, 2, 3), "Game start"));
cout << timemap[make_tuple(2,3,4)];
unordered_map<tuple<int,int,int>, string> timemap;
// 数据换顺序Little trick
int a, b, c;
tie(b, c, a) = make_tuple(a, b, c);
}
//不要滥用tuple,一旦发现tuple一再使用,考虑定义struct
C++11--Tuple类<tuple>的更多相关文章
- Python基础 之 tuple类-元组 和 dict类-字典
tuple 元组 一.tuple 类的基本属性 1.元组,有序:元素不可被修改,不能被增加或者删除tuple类 tu = (111,22,33,44) 一般写元组的时候,推荐在最后加入,和类方法进行区 ...
- C++11中的tuple应用:让函数返回多个值
在没有tuple之前,如果函数需要返回多个值,则必须定义一个结构体,有了C++11,可以基于tuple直接做了,下面是个示例: // 编译:g++ -std=c++11 -g -o x x.cpp # ...
- .Net 之Tuple 类
Tuple是什么 按照Msdn 上说:提供用于创造元组对象的静态方法.从字面意思并不能理解他的作用: Tuple 是个静态类,提供8个静态泛型方法:T 可以是值类型,也可是引用类型: 使用场景 ...
- python __builtins__ tuple类 (68)
68.'tuple', 转换为元组类型 class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple in ...
- ShoneSharp语言(S#)的设计和使用介绍系列(11)—“类”披炫服靓妆化成“表”
ShoneSharp语言(S#)的设计和使用介绍 系列(11)—“类”披炫服靓妆化成“表” 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/Sh ...
- C++11模板类使用心得
1.推荐使用std::shared_ptr<TaskT>代替指针TaskT*使用,shared_ptr是一种智能指针,能自主销毁释放内存,在c++11中被引入,在多线程编程中有很大的用处, ...
- c++11 时间类 std::chrono
概念: chrono库:主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration:表示一段时间间隔,用来记录时间长度,可以表示几秒钟.几分钟或者几 ...
- Python Tuple(元组) tuple()方法
描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- ...
- 学习C++.Primer.Plus 11 使用类
1.操作符重载 重载操作符的几个限制: a) 重载的至少有一个操作数是用户定义的类型,这将防止用户为标准类型重载操作符. b) 不能违反操作符原有来的句法规则. c) ...
随机推荐
- 螺旋图 comet3 (comet) 不同轴的圆周运动图
matlab 绘图 螺旋图小实例 动态显示comet3函数(comet显示平面) t=[:]; x=*t*sin(pi/).*cos(*t); y=*t*sin(pi/).*sin(*t); z=* ...
- xdoj1321----简单搜索
1321: 营救公主 时间限制: 1 Sec 内存限制: 128 MB提交: 156 解决: 37[提交][状态][讨论版] 题目描述 DSKer今天又做梦了,他的睡眠质量一直很差.他梦见他化身骑 ...
- java基本数据类型的范围
System.out.println("BYTE MAX_VALUE = " + Byte.MAX_VALUE); System.out.println("BYTE MI ...
- 深入学习Motan系列(二)——服务发布
闯关经验: 袋鼠走过了第一关,顺利搭建出了Demo,信心爆棚.不过之后,心想怎么去研究这个框架呢.查了一下,官方文档,好像没什么东西可以研究啊.后来,又搜了搜博客,因为这是微博的框架嘛,所以搜索时用百 ...
- Centos7提示swap交换空间不足解决方法
一张图就能解决的问题,就不多bb了
- C++中的ravalue学习笔记
一.学习笔记 1. A a = 42; 会先以42为参数构造一个A类对象,然后调用拷贝构造函数来构造a,目前编译器优化掉了拷贝构造函数的调用,测试拷贝构造函数是没有被调用的,但是其权限不能为priva ...
- day2 python学习
1.格式化输出 现有一练习需求,问用户的姓名.年龄.工作.爱好 ,然后打印成以下格式 复制代码 ------------ info of Alex Li ----------- Name : Alex ...
- DevExpress的DateEdit控件正确显示日期的周名称
DevExpress 的控件相当好看而且很好用,但 DateEdit 在是显示周名时,只能显示一个“星”字. 以下是解决方法,此解决方法不需修改其源码,所以免去了重新编译的必要,可直接使用其发布的标准 ...
- msyql多个or,and,
where (`to`= 2 and `from`= 8) or (`to`= 8 and `from`= 2) and from_unixtime(a.time,'%Y-%m-%d') ='2017 ...
- HTTP redirect 重定向到 HTTPS
最近帮一个顾客做网站, 需要HTTPS. 之前接触的SSL certificate直接上传到plesk 上面勾选重定向即可. 在此先吐槽下godaddy 服务贵功能还少. 用代码从HTTP来重定向到 ...