当前位置:网站首页>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
边栏推荐
- [Lua language from bronze to king] Part 2: development environment construction +3 editor usage examples
- How VPN works
- 随机数备注
- L2tp/ipsec one click installation script
- 洛谷 P1051 谁拿了最多奖学金
- RDD的执行原理
- Inline element, block element, inline block element
- IndexError: Target 7 is out of bounds.
- LeetCode 207:课程表(拓扑排序判断是否成环)
- 向量操作与坐标转换相关方法
猜你喜欢
随机推荐
开放合作,共赢未来 | 福昕鲲鹏加入金兰组织
[special session] SME growth plan - ECS special session
L2tp/ipsec one click installation script
解决 These dependencies were not found: * core-js/modules/es6.array.fill in xxx 之类的问题
Global and Chinese market of anion sanitary napkins 2022-2028: Research Report on technology, participants, trends, market size and share
Terminal network in VPN client connection settings of router
Experience of Shenzhou computer
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
LeetCode 515 在每个数行中找最大值[BFS 二叉树] HERODING的LeetCode之路
Mysql database recovery case sharing
Shell script for MySQL real-time synchronization of binlog
.jar中没有主清单属性
Explain the input attribute in PHP (hide the read-only restriction)
Global and Chinese market of water massage column 2022-2028: Research Report on technology, participants, trends, market size and share
C# Lambda
The startup mode of cloudbase init is \Cloudbase init has hidden dangers
Obtain the package name, application name, icon, etc. of the uninstalled APK through packagemanager. There is a small message
A summary of the posture of bouncing and forwarding around the firewall
简单的折射效果
屏幕截图推荐—Snipaste