std::chrono::weekday::operator+=, std::chrono::weekday::operator-=

来自cppreference.com
< cpp‎ | chrono‎ | weekday
 
 
工具库
通用工具
格式化库 (C++20)
(C++11)
关系运算符 (C++20 中弃用)
整数比较函数
(C++20)(C++20)(C++20)
(C++20)
swap 与类型运算
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
常用词汇类型
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)

初等字符串转换
(C++17)
(C++17)
栈踪
 
 
 
constexpr std::chrono::weekday& operator+=(const std::chrono::days& d) noexcept;
(1) (C++20 起)
constexpr std::chrono::weekday& operator-=(const std::chrono::days& d) noexcept;
(2) (C++20 起)

对星期之日值加或减 d.count() ,将结果对 7 取余,规约成范围 [0, 6] 中的整数。

1) 进行 *this = *this + d;
2) 进行 *this = *this - d;

返回值

到修改后的此 weekday 的引用。

注意

调用这些函数之一后,若运算中不出现溢出,则 ok() 始终为 true 。

示例

#include <iostream>
#include <chrono>
 
int main()
{
    std::cout << std::boolalpha;
 
    std::chrono::weekday wd {5}; // Friday is 5
    wd += std::chrono::days(2);
    std::cout << (wd == std::chrono::weekday(0)) << ' '
              << (wd == std::chrono::Sunday) << ' ';
 
    wd -= std::chrono::days(3);
    std::cout << (wd == std::chrono::weekday(4)) << ' '
              << (wd == std::chrono::Thursday) << ' ';
 
    wd = std::chrono::Wednesday;
    wd += std::chrono::days{8}; // ((3 += 8 == 11) % 7) == 4;
    std::cout << (wd == std::chrono::Thursday) << ' ';
 
    wd -= (std::chrono::Sunday - std::chrono::Tuesday); // -= -2
    // (4 -= -2) == 6
    std::cout << (wd == std::chrono::Saturday) << '\n';
}

输出:

true true true true true true

参阅

自增或自减 weekday
(公开成员函数)
weekday 上进行算术
(函数)