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) ...
随机推荐
- shell常用函数封装-main.sh
#!/bin/bash #sunlight sp monitor system #created on 2018/01/07#by chao.dong#used by sp servers consi ...
- url的反向解析
1. url的语法格式: url(regex, views, **kwargs, name) name:为地址起别名,反向解析时使用 2.反向解析 对于Django中的url反向解析,是分模板和视图的 ...
- HDU - 5201 :The Monkey King (组合数 & 容斥)
As everyone known, The Monkey King is Son Goku. He and his offspring live in Mountain of Flowers and ...
- http解析过程
HTTP协议定义Web客户端如何从Web服务器请求Web页面,以及服务器如何把Web页面传送给客户端.HTTP协议采用了请求/响应模型.客户端向服务器发送一个请求报文,请求报文包含请求的方法.URL. ...
- Spring mvc下载文件java代码
/** * 下载模板文件 * @author cq */ @RequestMapping("/downloadExcel.do") public ResponseEntity< ...
- 【BZOJ4720】【NOIP2016】换教室
我当年真是naive…… 原题: 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程.在可以选择的课程中,有2n节 课程安排在n个时间段上.在第i(1≤i≤n)个时间段上,两节 ...
- 【git】如何向gitHub上推送自己的项目
一.在本地建立项目spring 二.在gitHub上创建spring仓库 三.在本地生成公私钥文件 命令:ssh-keygen -t rsa -C "shangxiaofei3@163.co ...
- Arrays类的使用
定义 : 此类包含用来操作数组(比如排序和搜索)的各种方法.使用需要导入import java.util.Arrays;包 1.sort方法(对数组进行升序排列) public class Array ...
- debezium mongodb 集成测试
debezium 是一个方便的cdc connector 可以帮助我们解决好多数据实时变更处理.数据分析.微服务的数据通信 从上次跑简单demo到现在,这个工具是有好多的变更,添加了好多方便的功能,支 ...
- Unity 代码 学习
A B C D E F G H I J K L M N O P Q R S T U V W X Y Z A: B: using System; using System.Runtime.Compile ...