std::chrono::weekday::operator+=, std::chrono::weekday::operator-=
来自cppreference.com
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 (公开成员函数) | |
(C++20) |
在 weekday 上进行算术 (函数) |