STL其他--<tuple>用法【C11】
tuple 库
tuple 库 是能够将不同类型的对象组合起来形成一个对象。和pair 对象一样,但是可以组织多种不同类型的元素。
tuple中的元素类型是在编译时期决定的。与<utility>中的pair 类密切相关, pair对象可以视为tuple类型对待。
头文件: <tuple>,
1. tuple 类
原型: template <class... Types> class tuple;
tuple对象可以组合不同元素, 每个元素的类型可以不同;
2. 成员函数
(constructor)
(1)默认构造函数,
构造tuple对象用元素初始值;
(2)copy/move 构造函数
通过另一个tuple对象来构造tuple对象
(3)隐式转换构造函数;
(4)初始自定义构造函数;
(5)pair 转化 构造函数
示例:
// tuple constructors
#include <iostream> // std::cout
#include <utility> // std::make_pair
#include <tuple> // std::tuple, std::make_tuple, std::get int main ()
{
std::tuple<int,char> first; // default
std::tuple<int,char> second (first); // copy
std::tuple<int,char> third (std::make_tuple(,'b')); // move
std::tuple<long,char> fourth (third); // implicit conversion
std::tuple<int,char> fifth (,'a'); // initialization
std::tuple<int,char> sixth (std::make_pair(,'c')); // from pair / move std::cout << "sixth contains: " << std::get<>(sixth);
std::cout << " and " << std::get<>(sixth) << '\n'; return ;
}
operator= 赋值函数
tuple:: swap 函数
void swap (tuple& tpl)
交换参数中的每个元素值。
3. Non-member function overloads
(1)逻辑比较
顺序比较两个不同的元素,指导发面不同,即作为结果。返回bool值。
(2) std::swap(a,b);
(3) std::get<i>(mytuple)
返回tuple 对象中的第i个元素的引用。
get同样用于tuple类似的类型,如pair, array等。
注意, 模板参数i来决定获取的索引位置,且必须是constexpr,即编译器为const value
返回参会参数是: tuple_element<i, tuple<type1, type2,...>>:: type 是 type对象中第i个元素的类型。
4. Helper classes
(1)std::tuple_size<T>类
用来获取tuple 对象中的元素数量的类模板。
模板参数T ,用于访问的tuple对象的类型。 即一个已经存在的类的声明。
类成员变量:value, 即类型T中的元素数量。
/ tuple_size
#include <iostream> // std::cout
#include <tuple> // std::tuple, std::tuple_size int main ()
{
std::tuple<int,char,double> mytuple (,'a',3.14); std::cout << "mytuple has ";
std::cout << std::tuple_size<decltype(mytuple)>::value;
std::cout << " elements." << '\n'; return ;
}
(2)std::tuple_element<>类
被设计用来访问tuple对象中第i个元素类型的类。
具有成员类型 type, 即通过std::get<i>(tuple)访问第i个元素的类型。
#include <iostream> // std::cout
#include <tuple> // std::tuple, std::make_tuple, std::tuple_element, std::get int main ()
{
auto mytuple = std::make_tuple (,'a'); std::tuple_element<,decltype(mytuple)>::type first = std::get<>(mytuple);
std::tuple_element<,decltype(mytuple)>::type second = std::get<>(mytuple); std::cout << "mytuple contains: " << first << " and " << second << '\n'; return ;
}
5.Functions
生成对象:
std::make_tuple<class... Types>
构建一个合适tuple类型的对象,并包含执行元素类型的参数。
函数返回的类型tuple<type,,,> 是在函数模板中定义的参数。
函数通过call tuple的初始构造函数。
// make_tuple example
#include <iostream>
#include <tuple>
#include <functional> int main()
{
auto first = std::make_tuple (,'a'); // tuple < int, char > const int a = ; int b[]; // decayed types:
auto second = std::make_tuple (a,b); // tuple < int, int* > auto third = std::make_tuple (std::ref(a),"abc"); // tuple < const int&, const char* > std::cout << "third contains: " << std::get<>(third);
std::cout << " and " << std::get<>(third);
std::cout << std::endl; return ;
}
endl;
STL其他--<tuple>用法【C11】的更多相关文章
- C++中的STL中map用法详解(转)
原文地址: https://www.cnblogs.com/fnlingnzb-learner/p/5833051.html C++中的STL中map用法详解 Map是STL的一个关联容器,它提供 ...
- STL的其他用法(adjacent_find, find_first_of, sort_heap, merge, binary_search)总结
2017-08-20 17:26:07 writer:pprp 1.adjacent_find() 下面是源码实现: template <class ForwardIterator> Fo ...
- C++-STL:vector用法总结
目录 简介 用法 1. 头文件 2. vector的声明及初始化 3. vector基本操作 简介 vector,是同一类型的对象的集合,这一集合可看作可变大小的数组,是顺序容器的一种.相比于数组,应 ...
- STL中map用法
Map是 STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于 这个特性,它完成有可能在我们处理一对一数据的 ...
- [STL] SET实用用法
背景 今天考试深受平衡树之害,可以参见上一篇博客,想到了set却苦于实用的不熟练.同时QTY询问set的具体用法,所以写这篇博客,同时留作自用. 分类 参看了一下网上其他set博客,上来都是长篇大论概 ...
- C++中的STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据 处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时 ...
- C++ STL sort()函数用法
C++STL提供的在里的排序函数,有以下两种形式 此外还提供有稳定排序版本stable_sort(),用法类似. 第一种形式: template <class RandomAccessItera ...
- (转载) STL中map用法详解
Map是STL的一个关联容器,它提供一对一(其中第一个可以称为关键字,每个关键字只能在map中出现一次,第二个可能称为该关键字的值)的数据处理能力,由于这个特性,它完成有可能在我们处理一对一数据的时候 ...
- STL的常用用法、函数汇总(不定时更新)
隶书文字为原创. 1.vector 在c++中,vector是一个十分有用的容器,下面对这个容器做一下总结. 1 基本操作 (1)头文件#include<vector>. (2)创建vec ...
随机推荐
- SpringMVC之controller篇1
概述 继 Spring 2.0 对 Spring MVC 进行重大升级后,Spring 2.5 又为 Spring MVC 引入了注解驱动功能.现在你无须让 Controller 继承任何接口,无需在 ...
- 一个比较有意思的SDN网络技术相关blog/doc
http://feisky.xyz/sdn/linux/index.html 涵盖了目前主流的网络技术,所有比较有意思的内容全都覆盖了 SDN网络 目录 基本网络 TCP/IP标准模型 DHCP与DN ...
- (O)js核心:this
什么是this this是js中的一个关键词,它总是指向一个对象,而具体指向哪个对象是在运行时基于函数的执行环境动态绑定的,而非函数被声明时的环境. 当函数被调用时,this被添加到作用域中,例如: ...
- spring batch遇到的一些问题
1.Spring Batch - A job instance already exists: JobInstanceAlreadyCompleteException 这是因为JobParameter ...
- Python数据库工具类MySQLdb使用
MySQLdb模块用于连接mysql数据库. 基本操作 # -*- coding: utf-8 -*- #mysqldb import time, MySQLdb ...
- Windows8.1,IIS,WCF
在.NET 4中用IIS部署WCF就这么简单 http://www.cnblogs.com/dudu/archive/2011/01/18/1938490.html win8.1怎么安装iis htt ...
- Globalization and accessibility for tile and toast notifications (Windows Store apps)
http://msdn.microsoft.com/en-us/library/windows/apps/hh831183.aspx 做 HighContrast时,采用以下分目录方式: /Proje ...
- day13作业—(登录注册)
2.写一个函数完成三次登陆功能: 用户的用户名密码从一个文件register中取出. register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行. 完成三次验证, ...
- Spring mvc项目,使用jetty插件和tomcat路径相差一个项目名
pom.xml: jetty 插件配置: <plugin> <groupId>org.mortbay.jetty</groupId> <artifactId& ...
- Linux---CentOS 定时运行脚本配置
很多时候我们有希望服务器定时去运行一个脚本来触发一个操作,比如使用七牛的工具上传,如果同步文件里面有新增加一个文件,这个时候我们可以提供定时脚本去完成我们需要的同步命令(七牛的qrsbox工具是自动会 ...