1 분 소요

1. tie

template< class... Types >
std::tuple<Types&...> tie( Types&... args ) noexcept; // (since C++11)
template< class... Types >
constexpr std::tuple<Types&...> tie( Types&... args ) noexcept; // (until C++14)
  • pair, tuple로 된 요소들을 꺼내오는 함수
  • std::ignore를 사용해서 특정 index 자리 값은 무시할 수 있음

2. 코드로 알아보기

#include <iostream>
#include <string>
#include <set>
#include <tuple>
 
struct S {
    int n;
    std::string s;
    float d;
    bool operator<(const S& rhs) const
    {
        // n < rhs.n , s to rhs.s, d to rhs.d
        return std::tie(n, s, d) < std::tie(rhs.n, rhs.s, rhs.d);
    }
};

int main()
{
  std::set<S> set_of_s; // S is LessThanComparable

  S value{42, "Test", 3.14};
  std::set<S>::iterator iter;
  bool inserted;

  // unpacks the return value of insert into iter and inserted
  std::tie(iter, inserted) = set_of_s.insert(value);

  if (inserted)
      std::cout << "Value was inserted successfully\n";

  auto t = std::make_tuple(1, 2, 3);

  int x, y, z;
  std::tie(x, y, z) = t;
  std::cout << x << ' ' << y << ' ' << z << '\n';    //1 2 3

  x = y = z = 0;
  std::tie(x, y, std::ignore) = t;
  std::cout << x << ' ' << y << ' ' << z << '\n';    //1 2 0

  return 0;
}

참고

cppreference


This is personal diary for study documents.
Please comment if I'm wrong or missing something else 😄. 

Top

태그: , ,

카테고리:

업데이트:

댓글남기기