次の方法で共有


year_month クラス

月と年を表します。 日付は指定されません。

構文

class year_month; // C++20

メンバー

名前 説明
コンストラクター year_month を構築します
year 年を返します。
month 月を返します。
ok yearmonth の値が有効な範囲内にあるか確認します。
operator+= 指定した月数または年数を加算します。
operator-= 指定した月数または年数を減算します。

非メンバー

名前 説明
from_stream 指定した形式を使用して、ストリームから year_month を解析します。
operator+ 月または年を加算します。
operator- 月または年を減算します。
operator== 2 つの year_month 値が等しいかどうかを判断します。
operator<=> 2 つの year_month 値を比較します。 演算子 >, >=, <=, <, != はコンパイラによって合成されます。
operator<< year_month をストリームに出力します。

要件

ヘッダー:<chrono> (C++20 以降)

名前空間std::chrono:

コンパイラ オプション:/std:c++latest

コンストラクター

year_month を構築します。

1) year_month() = default;
2) constexpr year_month(const year& y, const month& m) noexcept;

パラメーター

y
year の値です。

m
month の値です。

解説

1) 既定のコンストラクターは、year または month の値を初期化しません。
2) 指定された値を使用して year_month を構築します。

日付を指定する C++20 構文の詳細については、以下を参照してください。 operator/

例: year_month を作成する

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym;
    return 0;
}
2021/Jun

month

月を取得します。

constexpr month month() const noexcept;

戻り値

month の値です。

year

年を取得します。

constexpr year year() const noexcept;

戻り値

year

ok

この year_month に格納されている年と月の値の両方が有効な範囲内にあるか確認します。

constexpr bool ok() const noexcept;

戻り値

year_month の年と月の値が有効な範囲内にある場合は true。 それ以外の場合は false

operator+=

この year_month に月または年を追加します。

1) constexpr year_month& operator+=(const months& dm) noexcept;
2) constexpr year_month& operator+=(const years& dy) noexcept;

パラメーター

dm
加算する月数。

dy
加算する年数。

戻り値

加算の結果を反映する *this

例: operator +=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym << '\n';

    ym += months{2};
    ym += years{1};

    std::cout << ym;
    
    return 0;
}
2021/Jun
2022/Aug

operator-=

この year_month から月または年を減算します。

1) constexpr year_month& operator-=(const months& dm) noexcept;
2) constexpr year_month& operator-=(const years& dy) noexcept;

パラメーター

dm
減算する月数。

dy
減算する年数。

戻り値

減算の結果を反映する *this

例: operator -=

// compile using: /std:c++latest
#include <iostream>
#include <chrono>

using namespace std::chrono;

int main()
{
    year_month ym{2021y / June};

    std::cout << ym << '\n';

    ym -= months{2};
    ym -= years{1};

    std::cout << ym;
    
    return 0;
}
2021/Jun
2020/Apr

関連項目

<chrono>
year
year_month_day
year_month_day_last
year_month_weekday
year_month_weekday_last
operator/
ヘッダー ファイル リファレンス