Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::tie

      From cppreference.com
      <cpp‎ |utility‎ |tuple
       
       
      Utilities library
       
       
      Defined in header<tuple>
      template<class...Types>
      std::tuple<Types&...> tie( Types&...args)noexcept;
      (since C++11)
      (constexpr since C++14)

      Creates a tuple of lvalue references to its arguments or instances ofstd::ignore.

      Contents

      [edit]Parameters

      args - zero or more lvalue arguments to construct the tuple from.

      [edit]Return value

      Astd::tuple object containing lvalue references.

      [edit]Possible implementation

      template<typename...Args>constexpr// since C++14std::tuple<Args&...> tie(Args&...args)noexcept{return{args...};}

      [edit]Notes

      std::tie may be used to unpack astd::pair becausestd::tuple has aconverting assignment from pairs:

      bool result;std::tie(std::ignore, result)= set.insert(value);

      [edit]Example

      1)std::tie can be used to introduce lexicographical comparison to a struct or to unpack a tuple;
      2)std::tie can work withstructured bindings:

      Run this code
      #include <cassert>#include <iostream>#include <set>#include <string>#include <tuple> struct S{int n;std::string s;float d; friendbool operator<(const S& lhs,const S& rhs)noexcept{// compares lhs.n to rhs.n,// then lhs.s to rhs.s,// then lhs.d to rhs.d// in that order, first non-equal result is returned// or false if all elements are equalreturn std::tie(lhs.n, lhs.s, lhs.d)< std::tie(rhs.n, rhs.s, rhs.d);}}; int main(){// Lexicographical comparison demo:std::set<S> set_of_s;     S value{42,"Test",3.14};std::set<S>::iterator iter;bool is_inserted; // Unpack a pair:    std::tie(iter, is_inserted)= set_of_s.insert(value);assert(is_inserted);  // std::tie and structured bindings:auto position=[](int w){returnstd::tuple(1* w,2* w);}; auto[x, y]= position(1);assert(x==1&& y==2);    std::tie(x, y)= position(2);// reuse x, y with tieassert(x==2&& y==4);  // Implicit conversions are permitted:std::tuple<char,short> coordinates(6,9);    std::tie(x, y)= coordinates;assert(x==6&& y==9); // Skip an element:std::string z;    std::tie(x,std::ignore, z)=std::tuple(1,2.0,"Test");assert(x==1&& z=="Test");}

      [edit]See also

      Structured binding(C++17) binds the specified names to sub-objects or tuple elements of the initializer[edit]
      (C++11)
      creates atuple object of the type defined by the argument types
      (function template)[edit]
      creates atuple offorwarding references
      (function template)[edit]
      (C++11)
      creates atuple by concatenating any number of tuples
      (function template)[edit]
      (C++11)
      placeholder to skip an element when unpacking atuple usingtie
      (constant)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/utility/tuple/tie&oldid=178881"

      [8]ページ先頭

      ©2009-2025 Movatter.jp