当前位置:网站首页>Tuple remarks
Tuple remarks
2022-06-24 07:48:00 【Fog dispels eyesight】
tuple( Tuples ) remarks
A tuple is an object that can hold a collection of elements . Each element can be of a different type .
c++ Standard library
tuple::operator=
tuple::swap
swap (tuple)
get (tuple)
Example :
// tuple example
#include <iostream> // std::cout
#include <tuple> // std::tuple, std::get, std::tie, std::ignore
int main ()
{
std::tuple<int,char> foo (10,'x');
auto bar = std::make_tuple ("test", 3.1, 14, 'y');
std::get<2>(bar) = 100; // access element
int myint; char mychar;
std::tie (myint, mychar) = foo; // unpack elements
std::tie (std::ignore, std::ignore, myint, mychar) = bar; // unpack (with ignore)
mychar = std::get<3>(bar);
std::get<0>(foo) = std::get<2>(bar);
std::get<1>(foo) = mychar;
std::cout << "foo contains: ";
std::cout << std::get<0>(foo) << ' ';
std::cout << std::get<1>(foo) << '\n';
return 0;
}
boost library tuple
The following references :https://blog.csdn.net/shudaqi2010/article/details/24895885
#include<iostream>
#include<string>
#include<boost/tuple/tuple.hpp>
#include<boost/tuple/tuple_io.hpp>
#include <boost/tuple/tuple_comparison.hpp>
using namespace std;
int main(){
//boost::tuple Expanded C++ Data type of std::pair To store more than two values .
// except boost::tuple, This chapter also covers classes boost::any and boost::variant To store values of uncertain types . among boost::any Variables of type are as flexible to use as variables in weakly typed languages . On the other hand , boost::variant Variables of type can store some predefined data types , Like we use union It's the same time .
typedef boost::tuple<std::string, std::string> person1;
person1 p1("Boris", "Schaeling");
cout << p1 << std::endl;
// It's like std::pair With auxiliary function std::make_pair() equally , A tuple can also use its auxiliary function boost::make_tuple() To create .
std::cout << boost::make_tuple("Boris", "Schaeling", 43) << std::endl;
// A tuple can also store values of reference types .
std::string s = "Boris";
std::cout << boost::make_tuple(boost::ref(s), "Schaeling", 43) << std::endl;
// because "Schaeling" and 43 It's delivered by value , So it is directly stored in tuples . What's different from them is : person The first element of is a pointer to s References to . Boost.Ref Medium boost::ref() Is used to create such a reference . Relative , To create a constant reference , You need to use boost::cref() .
// After learning how to create tuples , Let's look at how to access elements in tuples . std::pair Contains only two elements , So you can use the attribute first and second To access the elements . But tuples can contain an infinite number of elements , obviously , We need another way to solve the problem of access .
typedef boost::tuple<std::string, std::string, int> person2;
person2 p2 = boost::make_tuple("Boris", "Schaeling", 43);
std::cout << p2.get<0>() << std::endl;
std::cout << boost::get<0>(p2) << std::endl;
// We can access elements in tuples in two ways : Using member functions get() , Or pass tuples to an independent function boost::get() . When using both methods , The index values of elements are specified by template parameters . In the example, these two methods are used to access p The first element in . therefore , Boris Will be output twice .
// in addition , The validity of the index value is checked at compile time , Therefore, accessing illegal index values will cause compile time errors rather than runtime errors .
// Modification of elements in tuples , You can also use get() and boost::get() function .
typedef boost::tuple<std::string, std::string, int> person3;
person3 p3 = boost::make_tuple("Boris", "Schaeling", 43);
p3.get<1>() = "Becker";
std::cout << p3 << std::endl;
//get() and boost::get() Will return a reference value . In the example, we have modified lastname Then it will output : (Boris Becker 43) .
//Boost.Tuple In addition to overloading the stream operation operator , It also provides us with comparison operators . To use them , You must include the corresponding header file : boost/tuple/tuple_comparison.hpp .
typedef boost::tuple<std::string, std::string, int> person4;
person4 p4 = boost::make_tuple("Boris", "Schaeling", 43);
person4 p5 = boost::make_tuple("Boris", "Becker", 43);
std::cout << (p4 != p5) << std::endl;
// The above example will be output 1 Because two tuples p1 and p2 Is different .
}
Compiled output :
(Boris Schaeling)
(Boris Schaeling 43)
(Boris Schaeling 43)
Boris
Boris
(Boris Becker 43)
1
边栏推荐
猜你喜欢

光照使用的简单总结
![[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence](/img/c5/f548223666d7379a7d4aaed2953587.png)
[vulhub shooting range]] ZABBIX SQL injection (cve-2016-10134) vulnerability recurrence

Session & cookie details

Maxcompute remote connection, uploading and downloading data files

First acquaintance with JUC - day01

爬虫基础B1——Scrapy(B站学习笔记)

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

More than 60 million shovel excrement officials, can they hold a spring of domestic staple food?

火线,零线,地线,你知道这三根线的作用是什么吗?

Oracle-高级SQL限定查询
随机推荐
关于h5页面苹果手机使用fixed定位tabbar最底部时遮挡内容问题
『C语言』系统日期&时间
New features of PHP: bytecode cache and built-in server
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
pair类备注
智能指针备注
Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
UE common console commands
Q & A on cloud development cloudbase hot issues of "Huage youyue phase I"
科一易错点
A summary of the posture of bouncing and forwarding around the firewall
The describeregion interface of CVM is special and has two versions
New ways to play web security [6] preventing repeated use of graphic verification codes
Wechat cloud hosting hot issues Q & A
Deploy L2TP in VPN (medium)
Anaconda 中使用 You Get
希尔伯特-黄变换
洛谷 P1051 谁拿了最多奖学金
Group policy disables command prompt bypass
Moonwell Artemis现已上线Moonbeam Network