std::div, std::ldiv, std::lldiv, std::imaxdiv
来自 cppreference.cn
定义于头文件 <cstdlib> |
||
std::div_t div( int x, int y ); |
(1) | (constexpr since C++23) |
std::ldiv_t div( long x, long y ); |
(2) | (constexpr since C++23) |
std::lldiv_t div( long long x, long long y ); |
(3) | (since C++11) (constexpr since C++23) |
std::ldiv_t ldiv( long x, long y ); |
(4) | (constexpr since C++23) |
std::lldiv_t lldiv( long long x, long long y ); |
(5) | (since C++11) (constexpr since C++23) |
定义于头文件 <cinttypes> |
||
std::imaxdiv_t div( std::intmax_t x, std::intmax_t y ); |
(6) | (since C++11) (constexpr since C++23) |
std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y ); |
(7) | (since C++11) (constexpr since C++23) |
计算分子 x 除以分母 y 的商和余数。
(since C++11) |
商是代数商,任何小数部分都被丢弃(向零截断)。余数满足 quot * y + rem == x。 |
(C++11 前) |
商是表达式 x / y 的结果。余数是表达式 x % y 的结果。 |
(since C++11) |
目录 |
[编辑] 参数
x, y | - | 整数值 |
[编辑] 返回值
如果余数和商都可以表示为相应类型的对象(int, long, long long, std::intmax_t,分别),则返回两者作为类型为 std::div_t
, std::ldiv_t
, std::lldiv_t
, std::imaxdiv_t
的对象,定义如下
std::div_t
struct div_t { int quot; int rem; };
或
struct div_t { int rem; int quot; };
std::ldiv_t
struct ldiv_t { long quot; long rem; };
或
struct ldiv_t { long rem; long quot; };
std::lldiv_t
struct lldiv_t { long long quot; long long rem; };
或
struct lldiv_t { long long rem; long long quot; };
std::imaxdiv_t
struct imaxdiv_t { std::intmax_t quot; std::intmax_t rem; };
或
struct imaxdiv_t { std::intmax_t rem; std::intmax_t quot; };
如果余数或商之一无法表示,则行为未定义。
[编辑] 注意
在 CWG issue 614 被解决之前 (N2757),如果操作数之一为负数,则 内置除法和余数运算符 中商的舍入方向和余数的符号是实现定义的,但在 std::div
中是明确定义的。
在许多平台上,单个 CPU 指令可以同时获得商和余数,此函数可能会利用这一点,尽管编译器通常能够合并附近的 /
和 %
(如果合适)。
[编辑] 示例
运行此代码
#include <cassert> #include <cmath> #include <cstdlib> #include <iostream> #include <sstream> #include <string> std::string division_with_remainder_string(int dividend, int divisor) { auto dv = std::div(dividend, divisor); assert(dividend == divisor * dv.quot + dv.rem); assert(dv.quot == dividend / divisor); assert(dv.rem == dividend % divisor); auto sign = [](int n){ return n > 0 ? 1 : n < 0 ? -1 : 0; }; assert((dv.rem == 0) or (sign(dv.rem) == sign(dividend))); return (std::ostringstream() << std::showpos << dividend << " = " << divisor << " * (" << dv.quot << ") " << std::showpos << dv.rem).str(); } std::string itoa(int n, int radix /*[2..16]*/) { std::string buf; std::div_t dv{}; dv.quot = n; do { dv = std::div(dv.quot, radix); buf += "0123456789abcdef"[std::abs(dv.rem)]; // string literals are arrays } while (dv.quot); if (n < 0) buf += '-'; return {buf.rbegin(), buf.rend()}; } int main() { std::cout << division_with_remainder_string(369, 10) << '\n' << division_with_remainder_string(369, -10) << '\n' << division_with_remainder_string(-369, 10) << '\n' << division_with_remainder_string(-369, -10) << "\n\n"; std::cout << itoa(12345, 10) << '\n' << itoa(-12345, 10) << '\n' << itoa(42, 2) << '\n' << itoa(65535, 16) << '\n'; }
输出
+369 = +10 * (+36) +9 +369 = -10 * (-36) +9 -369 = +10 * (-36) -9 -369 = -10 * (+36) -9 12345 -12345 101010 ffff
[编辑] 参见
(C++11)(C++11) |
浮点除法运算的余数 (函数) |
(C++11)(C++11)(C++11) |
除法运算的带符号余数 (函数) |
(C++11)(C++11)(C++11) |
带符号余数以及除法运算的最后三位 (函数) |
C 文档 关于 div
|
[编辑] 外部链接
1. | 欧几里得除法 — 来自维基百科。 |
2. | 模运算(和截断除法) — 来自维基百科。 |