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) ...
随机推荐
- Unity 项目中委托Delegate用法案例
Unity中Delegate的用法场景 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar - ...
- next_permutation(start,end)
一道水题,简单的next_permutation用法,相同的还有prev_permutation 包含在头文件<algorithm>中 字符串 acab 含有两个a ,一个b ,一个c , ...
- Spring 注解详细分析解释有实例
概述 注释配置相对于 XML 配置具有很多的优势: 它可以充分利用 Java 的反射机制获取类结构信息,这些信息可以有效减少配置的工作.如使用 JPA 注释配置 ORM 映射时,我们就不需要指定 PO ...
- SQLI DUMB SERIES-4
less4 输入单引号发现回显正常,说明单引号被过滤了,输入双引号: ?id=1" 说明输入的Id被一对双引号和圆括号包围,因此闭合双引号和圆括号就行,其他方法跟less1差不多 例如:un ...
- Beta周第7次Scrum会议(11/16)【王者荣耀交流协会】
一.小组信息 队名:王者荣耀交流协会 小组成员 队长:高远博 成员:王超,袁玥,任思佳,王磊,王玉玲,冉华 小组照片 二.开会信息 时间:2017/11/16 17:03~17:17,总计14min. ...
- java学习之动手实验
一, 1,JAVA的基本运行单位是类 2,类的成员:成员变量,构造方法,普通方法和内部类 3,成员变量种类:字符类型:char 布尔类型:boolean 数值类型:byte, ...
- 浅谈log4j-6-xml配置 转自godtrue
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SY ...
- Java 枚举(enum) 详解4种常见的用法
JDK1.5引入了新的类型——枚举.在 Java 中它虽然算个“小”功能,却给我的开发带来了“大”方便. 大师兄我又加上自己的理解,来帮助各位理解一下. 用法一:常量 在JDK1.5 之前,我们定义常 ...
- Oracle数据库的学习
复制数据库结构到另外一数据库的的语句,首先在数据库创建链接,比如我在131数据库,dblink_018 的018为链接名称,随便取 ,可在此表查看数据库链接 select * from dba_db_ ...
- seelog 文件输出格式
项目中用到seelog,希望每个小时的记录输入在当前小时命名的日志,配置如下: <seelog> <outputs formatid="leads"> &l ...