#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>的更多相关文章

  1. Python基础 之 tuple类-元组 和 dict类-字典

    tuple 元组 一.tuple 类的基本属性 1.元组,有序:元素不可被修改,不能被增加或者删除tuple类 tu = (111,22,33,44) 一般写元组的时候,推荐在最后加入,和类方法进行区 ...

  2. C++11中的tuple应用:让函数返回多个值

    在没有tuple之前,如果函数需要返回多个值,则必须定义一个结构体,有了C++11,可以基于tuple直接做了,下面是个示例: // 编译:g++ -std=c++11 -g -o x x.cpp # ...

  3. .Net 之Tuple 类

    Tuple是什么 按照Msdn 上说:提供用于创造元组对象的静态方法.从字面意思并不能理解他的作用:   Tuple 是个静态类,提供8个静态泛型方法:T 可以是值类型,也可是引用类型:   使用场景 ...

  4. python __builtins__ tuple类 (68)

    68.'tuple', 转换为元组类型 class tuple(object) | tuple() -> empty tuple | tuple(iterable) -> tuple in ...

  5. ShoneSharp语言(S#)的设计和使用介绍系列(11)—“类”披炫服靓妆化成“表”

    ShoneSharp语言(S#)的设计和使用介绍 系列(11)—“类”披炫服靓妆化成“表” 作者:Shone 声明:原创文章欢迎转载,但请注明出处,https://www.cnblogs.com/Sh ...

  6. C++11模板类使用心得

    1.推荐使用std::shared_ptr<TaskT>代替指针TaskT*使用,shared_ptr是一种智能指针,能自主销毁释放内存,在c++11中被引入,在多线程编程中有很大的用处, ...

  7. c++11 时间类 std::chrono

    概念: chrono库:主要包含了三种类型:时间间隔Duration.时钟Clocks和时间点Time point. Duration:表示一段时间间隔,用来记录时间长度,可以表示几秒钟.几分钟或者几 ...

  8. Python Tuple(元组) tuple()方法

    描述 Python 元组 tuple() 函数将列表转换为元组.每组词 www.cgewang.com 语法 tuple()方法语法: tuple( iterable ) 参数 iterable -- ...

  9. 学习C++.Primer.Plus 11 使用类

    1.操作符重载 重载操作符的几个限制: a)         重载的至少有一个操作数是用户定义的类型,这将防止用户为标准类型重载操作符. b)         不能违反操作符原有来的句法规则. c)  ...

随机推荐

  1. 【转&改进】Linux MPI 单机配置

    MPI的全称是Message Passing Interface即标准消息传递界面,可以用于并行计算.MPI有多种实现版本,如MPICH, CHIMP以及OPENMPI.这里我们采用MPICH版本. ...

  2. Unity打包/读取AssetBundle资源全教程

    Unity 资源AssetBundle打包 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar ...

  3. personal的制作

    <style>  .personal{   float: right;   height: 40px;   line-height: 40px;   width: 200px;  }  . ...

  4. event.currentTarget和event.target的区别

    currentTarget始终是监听事件者,而target是事件的真正发出者.

  5. linux下文件校验的使用

    为解决官方发布的软件包被别人更改或者软件在传输过程中出现传输错误等问题,软件官方在提供软件包的同时,还提供一个保存MD5校验码的文件. Linux/unix中可以使用如下命令获得校验码和官方的校验码对 ...

  6. EasyUI datagrid 一个可以 直接运行例子一个文件 六

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...

  7. java 自带的工具

    前辈说,java的基本功的好坏,一个方面要看是否能熟练使用jdk bin下的工具使用情况. 自己整理一下使用的工具. ■ javac 一个编译java的工具,进入java所在文件的路径后,javac ...

  8. day39Python

     1.Python简介 Python 是一个高层次的结合了解释性.编译性.互动性和面向对象的脚本语言. Python 的设计具有很强的可读性,相比其他语言经常使用英文关键字,其他语言的一些标点符号,它 ...

  9. linux日志管理

    //有关当前登录用户的信息记录在文件utmp中 //登录进入和退出纪录在文件wtmp中 [root@bogon python]# who //who命令查询utmp文件并报告当前登录的每个用户 /va ...

  10. java_main

    Java中用户向系统传递参数的三种基本方式 main方法 在Java中,main()方法是Java应用程序的入口方法,也就是说,程序在运行的时候,第一个执行的方法就是main()方法,这个方法和其他的 ...