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) ...
随机推荐
- XXS level4
(1)查看PHP源代码 <?php ini_set("display_errors", 0); $str = $_GET["keyword"]; $str ...
- es6 set&sort
es6提供了新的数据结构Set. 它类似于数组,但是成员的值都是唯一的,没有重复的值. Set函数可以接受一个数组(或类似数组的对象)作为参数,用来初始化. 1.set去重 首先我们 let 一个数组 ...
- zzuli2226:神奇的薯条
题目描述 小明拿了n元钱去买薯条,薯条小份3元,大份7元.现在小明想知道如果只买薯条,自己的钱是否可以刚好花完,请你设计一个程序帮他计算一下. 输入 第一行输入一个整数T,表示实例数量.(1<= ...
- Elasticsearch基本用法(1)--原生操作
2.2.创建索引 2.2.1.语法 创建索引的请求格式: 请求方式:PUT 请求路径:/索引库名 请求参数:json格式: { "settings": { "number ...
- C++学习(十八)(C语言部分)之 指针2
指针1.指针的概述 指针是什么? 指针是一个地址 是一个常量 int 整型 int a a是变量 指针用来做什么? 方便使用数组或者字符串 像汇编语言一样处理内存地址2.指针变量 什么是指针变量? 是 ...
- 20165313 《Java程序设计》第三周学习总结
教材学习总结 这一章主要讲解了类的创建与使用,以及其中参数的调用方式,如何将多个对象组合,包的用法,访问权的设置和基本类封装. 1.对象注意初始化 2.包语句使用后要把对应得.java文件放到与包同名 ...
- MongoDB高可用集群搭建(主从、分片、路由、安全验证)
目录 一.环境准备 1.部署图 2.模块介绍 3.服务器准备 二.环境变量 1.准备三台集群 2.安装解压 3.配置环境变量 三.集群搭建 1.新建配置目录 2.修改配置文件 3.分发其他节点 4.批 ...
- java黑魔法-反射机制-02-通过Java反射调用其他类方法
package com.aaron.reflect; import java.lang.reflect.Method; import java.lang.reflect.InvocationTarge ...
- 世界各个地区WIFI 2.4G及5G信道划分表(附无线通信频率分配表)
参考:https://blog.csdn.net/dxpqxb/article/details/80969760 目前主流的无线WIFI网络设备802.11a/b/g/n/ac: 传统 802.11 ...
- mino 路径格式的bucket 数据访问
实施上这个功能很简答,如果官方不支持,我们可以通过基于nginx 的url rewrite 也可以实现 格式说明 如果配置了domain minio 会将 http://mydomain.com/bu ...