当前位置:网站首页>tuple(元组)备注
tuple(元组)备注
2022-06-24 06:44:00 【雾散睛明】
tuple(元组)备注
元组是能够容纳元素集合的对象。每个元素可以是不同的类型。
c++ 标准库
tuple::operator=
tuple::swap
swap (tuple)
get (tuple)
示例:
// 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库tuple
以下参考: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 扩展了 C++ 的数据类型 std::pair 用以储存多个而不只是两个值。
//除了 boost::tuple, 这一章还涵盖了类 boost::any 和 boost::variant 以储存那些不确定类型的值。 其中 boost::any 类型的变量使用起来就像弱类型语言中的变量一样灵活。 另一方面, boost::variant 类型的变量可以储存一些预定义的数据类型, 就像我们用 union 时候一样。
typedef boost::tuple<std::string, std::string> person1;
person1 p1("Boris", "Schaeling");
cout << p1 << std::endl;
//就像 std::pair 有辅助函数 std::make_pair() 一样, 一个元组也可以用它的辅助函数 boost::make_tuple() 来创建。
std::cout << boost::make_tuple("Boris", "Schaeling", 43) << std::endl;
//一个元组也可以存储引用类型的值。
std::string s = "Boris";
std::cout << boost::make_tuple(boost::ref(s), "Schaeling", 43) << std::endl;
//因为 "Schaeling" 和 43 是按值传递的,所以就直接存储在了元组中。 与他们不同的是: person 的第一个元素是一个指向 s 的引用。 Boost.Ref 中的 boost::ref() 就是用来创建这样的引用的。 相对的, 要创建一个常量的引用的时候, 你需要使用 boost::cref() 。
//在学习了创建元组的方法之后, 让我们来了解一下访问元组中元素的方式。 std::pair 只包含两个元素, 故可以使用属性 first 和 second 来访问其中的元素。 但元组可以包含无限多个元素, 显然, 我们需要用另一种方式来解决访问的问题。
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;
//我们可以用两种方式来访问元组中的元素: 使用成员函数 get() , 或者将元组传给一个独立的函数 boost::get() 。 使用这两种方式时, 元素的索引值都是通过模板参数来指定的。 例子中就分别使用了这两种方式来访问 p 中的第一个元素。 因此, Boris 会被输出两次。
//另外, 对于索引值合法性的检查会在编译期执行, 故访问非法的索引值会引起编译期错误而不是运行时的错误。
//对于元组中元素的修改, 你同样可以使用 get() 和 boost::get() 函数。
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() 和 boost::get() 都会返回一个引用值。 例子中修改了 lastname 之后将会输出: (Boris Becker 43) 。
//Boost.Tuple 除了重载了流操作运算符以外, 还为我们提供了比较运算符。 为了使用它们, 你必须要包含相应的头文件: 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;
//上面的例子将会输出 1 因为两个元组 p1 和 p2 是不同的。
}
编译后输出:
(Boris Schaeling)
(Boris Schaeling 43)
(Boris Schaeling 43)
Boris
Boris
(Boris Becker 43)
1
边栏推荐
- How VPN works
- get_ started_ 3dsctf_ two thousand and sixteen
- What is automated testing? What software projects are suitable for automated testing?
- A penetration test of c/s Architecture - Request encryption, decryption and test
- Spark stage and shuffle for daily data processing
- 什么是CC攻击?如何判断网站是否被CC攻击? CC攻击怎么防御?
- 向量操作与坐标转换相关方法
- 相機標定(標定目的、原理)
- Face pincher: a hot meta universe stylist
- 相机标定(标定目的、原理)
猜你喜欢
![[Proteus] Arduino uno + ds1307+lcd1602 time display](/img/96/d8c1cacc8a633c679b1a58a1eb8cb9.png)
[Proteus] Arduino uno + ds1307+lcd1602 time display

jarvisoj_level2

Buuctf misc grab from the doll

bjdctf_ 2020_ babystack
![Selector (>, ~, +, [])](/img/7e/2becfcf7a7b2e743772deee5916caf.png)
Selector (>, ~, +, [])

MySQL - three tables (student, course, score) to query the name, number and score of students whose course is mathematics

20 not to be missed ES6 tips

Win10 build webservice
![[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code](/img/b3/26cfa385aa357c3a7a77e9db47e94c.png)
[image feature extraction] image feature extraction based on pulse coupled neural network (PCNN) including Matlab source code

阿里云全链路数据治理
随机推荐
Event related | reveal how Ti-One's support ability for large-scale events is developed
Global and Chinese market of basketball uniforms 2022-2028: Research Report on technology, participants, trends, market size and share
What is the mentality of spot gold worth learning from
PIP install XXX on the terminal but no module named XXX on pycharm
[image fusion] image fusion based on NSST and PCNN with matlab code
Description of module data serial number positioning area code positioning refers to GBK code
Lend you a pair of insight, Frida native trace
[frame rate doubling] development and implementation of FPGA based video frame rate doubling system Verilog
Global and Chinese market of bed former 2022-2028: Research Report on technology, participants, trends, market size and share
Camera calibration (calibration purpose and principle)
Combine with (& &) logic or (||), dynamic binding and ternary operation
2.1.1 QML grammar foundation I
Black box and white box models for interpretable AI
A case of bouncing around the system firewall
[TS] function type
RDD的执行原理
屏幕截图推荐—Snipaste
Only two lines are displayed, and the excess part is displayed with Ellipsis
How to turn on win11 notebook power saving mode? How to open win11 computer power saving mode
向量操作与坐标转换相关方法