当前位置:网站首页>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
原网站

版权声明
本文为[Fog dispels eyesight]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/175/202206240252198171.html