STL其他--<tuple>用法【C11】

本文深入介绍了C++标准库中的tuple类及其使用方法,包括构造函数、成员函数、非成员函数重载等内容,并提供了实例帮助理解。

 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(20,'b'));   // move
  std::tuple<long,char> fourth (third);                   // implicit conversion
  std::tuple<int,char> fifth (10,'a');                    // initialization
  std::tuple<int,char> sixth (std::make_pair(30,'c'));    // from pair / move

  std::cout << "sixth contains: " << std::get<0>(sixth);
  std::cout << " and " << std::get<1>(sixth) << '\n';

  return 0;
}

 

 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 (10,'a',3.14);

  std::cout << "mytuple has ";
  std::cout << std::tuple_size<decltype(mytuple)>::value;
  std::cout << " elements." << '\n';

  return 0;
}

(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 (10,'a');

  std::tuple_element<0,decltype(mytuple)>::type first = std::get<0>(mytuple);
  std::tuple_element<1,decltype(mytuple)>::type second = std::get<1>(mytuple);

  std::cout << "mytuple contains: " << first << " and " << second << '\n';

  return 0;
}

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 (10,'a');             // tuple < int, char >

  const int a = 0; int b[3];                         // 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<0>(third);
  std::cout << " and " << std::get<1>(third);
  std::cout << std::endl;

  return 0;
}

endl;

 

转载于:https://www.cnblogs.com/icmzn/p/8656989.html

<?xml version="1.0" encoding="utf-8"?> <!-- This URDF was automatically created by SolidWorks to URDF Exporter! Originally created by Stephen Brawner (brawner@gmail.com) Commit Version: 1.6.0-4-g7f85cfe Build Version: 1.6.7995.38578 For more information, please see http://wiki.ros.org/sw_urdf_exporter --> <robot name="旋转云台"> <link name="base_link"> <inertial> <origin xyz="3.4694E-18 0.005 -1.0639E-18" rpy="0 0 0" /> <mass value="0.012566" /> <inertia ixx="1.3614E-06" ixy="-2.4284E-39" ixz="-3.1862E-54" iyy="2.5133E-06" iyz="2.5633E-38" izz="1.3614E-06" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/base_link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/base_link.STL" /> </geometry> </collision> </link> <link name="rod_Link"> <inertial> <origin xyz="3.4694E-18 0.05 1.534E-19" rpy="0 0 0" /> <mass value="0.017671" /> <inertia ixx="1.4975E-05" ixy="-1.2806E-38" ixz="0" iyy="4.9701E-07" iyz="-2.2959E-38" izz="1.4975E-05" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/rod_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/rod_Link.STL" /> </geometry> </collision> </link> <joint name="rod_joint" type="continuous"> <origin xyz="0 0 0" rpy="0 0 0" /> <parent link="base_link" /> <child link="rod_Link" /> <axis xyz="0 1 0" /> </joint> <link name="slide_Link"> <inertial> <origin xyz="0.022137 0.005 0.0036181" rpy="0 0 0" /> <mass value="0.0048703" /> <inertia ixx="1.5382E-07" ixy="4.6524E-22" ixz="-2.3216E-07" iyy="1.609E-06" iyz="8.7344E-23" izz="1.5363E-06" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/slide_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/slide_Link.STL" /> </geometry> </collision> </link> <joint name="slide_joint" type="prismatic"> <origin xyz="0 0.019219 0" rpy="0 0 0" /> <parent link="rod_Link" /> <child link="slide_Link" /> <axis xyz="0 1 0" /> <limit lower="-0.01" upper="0.066" effort="100" velocity="1" /> </joint> <link name="spin_Link"> <inertial> <origin xyz="-0.00022514 0.0091203 -0.0093002" rpy="0 0 0" /> <mass value="0.0019747" /> <inertia ixx="1.5576E-07" ixy="9.1774E-10" ixz="-1.1079E-09" iyy="7.0217E-08" iyz="3.791E-08" izz="1.1002E-07" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/spin_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/spin_Link.STL" /> </geometry> </collision> </link> <joint name="spin_joint" type="continuous"> <origin xyz="0.045398 0.01 0.0074199" rpy="0 0 0" /> <parent link="slide_Link" /> <child link="spin_Link" /> <axis xyz="0 1 0" /> </joint> <link name="holder_Link"> <inertial> <origin xyz="0.0038019 0.004 0.0056678" rpy="0 0 0" /> <mass value="0.0032821" /> <inertia ixx="2.4188E-07" ixy="6.8662E-24" ixz="-1.6276E-09" iyy="4.4741E-07" iyz="-1.2117E-24" izz="2.4054E-07" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/holder_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/holder_Link.STL" /> </geometry> </collision> </link> <joint name="holder_joint" type="fixed"> <origin xyz="-0.00029041 0.022 -0.011996" rpy="0 -0.58 0" /> <parent link="spin_Link" /> <child link="holder_Link" /> <axis xyz="0 1 0" /> </joint> <link name="camera_Link"> <inertial> <origin xyz="-0.0050045 -0.0021482 0.0051052" rpy="0 0 0" /> <mass value="0.0005554" /> <inertia ixx="6.9206E-09" ixy="8.1318E-10" ixz="1.1082E-09" iyy="7.3993E-09" iyz="1.2123E-09" izz="7.8293E-09" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/camera_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://yuntai_meshes/camera_Link.STL" /> </geometry> </collision> </link> <joint name="camera_joint" type="continuous"> <origin xyz="0.017512 0.004 0.01354" rpy="0 0 0" /> <parent link="holder_Link" /> <child link="camera_Link" /> <axis xyz="0.83046 0 -0.55707" /> </joint> <link name="view_Link"> <inertial> <origin xyz="0 1.7347E-18 0.001" rpy="0 0 0" /> <mass value="6.2832E-06" /> <inertia ixx="3.6652E-12" ixy="0" ixz="-1.8714E-44" iyy="3.6652E-12" iyz="-1.2142E-44" izz="3.1416E-12" /> </inertial> <visual> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://meshes/view_Link.STL" /> </geometry> <material name=""> <color rgba="0.79216 0.81961 0.93333 1" /> </material> </visual> <collision> <origin xyz="0 0 0" rpy="0 0 0" /> <geometry> <mesh filename="package://meshes/view_Link.STL" /> </geometry> </collision> </link> <joint name="view_joint" type="fixed"> <origin xyz="0.005 0.004 0" rpy="1.5708 0 1.4373" /> <parent link="camera_Link" /> <child link="view_Link" /> <axis xyz="0 0 0" /> </joint> </robot> 这个可以正常加载
最新发布
09-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值