最終更新日時(UTC):
が更新

履歴 編集

function template
<tuple>

std::operator<(C++11)

namespace std {
  // operator<=>により、以下の演算子が使用可能になる (C++20)
  template<class... TTypes, class... UTypes>
  bool operator<(const tuple<TTypes...>& t,
                 const tuple<UTypes...>& u); // (1) C++11

  template<class... TTypes, class... UTypes>
  bool operator<(const tuple<TTypes...>& t,
                 const tuple<UTypes...>& u); // (1) C++14
}

概要

2つのtupleにおいて、左辺が右辺より小さいかの比較を行う。

要件

  • 2つのtupleの要素数が同じであること。
  • tupleの要素std::get<i>(t)std::get<i>(u)において、すべての要素の比較 std::get<i>(t) < std::get<i>(u) および std::get<i>(u) < std::get<i>(t) の比較結果がboolに変換可能な型であること。

戻り値

2つのtupleオブジェクト、tuの辞書順比較を行った結果を返す。定義は以下のようになる:

(bool)(get<0>(t) < get<0>(u)) || (!(bool)(get<0>(u) < get<0>(t)) && t_tail < u_tail)

ただし、r_tailは、あるtupleオブジェクトrの最初の要素以外の全ての要素を含むtupleオブジェクトを表す。

2つのtupleオブジェクトの要素数が0である場合は、falseを返す。

#include <iostream>
#include <tuple>
#include <string>

int main()
{
  std::tuple<int, char, std::string> t1(1, 'a', "hello");
  std::tuple<int, char, std::string> t2(2, 'b', "world");

  std::cout << std::boolalpha;
  {
    bool result = t1 < t2;
    std::cout << result << std::endl;
  }
  {
    bool result = t2 < t1;
    std::cout << result << std::endl;
  }
}

出力

true
false

バージョン

言語

  • C++11

処理系

関連項目

参照