std:: div, std:: ldiv, std:: lldiv, std:: imaxdiv
|
定义于头文件
<cstdlib>
|
||
|
std
::
div_t
div
(
int
x,
int
y
)
;
|
(1) | (C++23 起 constexpr) |
|
std
::
ldiv_t
div
(
long
x,
long
y
)
;
|
(2) | (C++23 起 constexpr) |
|
std
::
lldiv_t
div
(
long
long
x,
long
long
y
)
;
|
(3) |
(C++11 起)
(C++23 起 constexpr) |
|
std
::
ldiv_t
ldiv
(
long
x,
long
y
)
;
|
(4) | (C++23 起 constexpr) |
|
std
::
lldiv_t
lldiv
(
long
long
x,
long
long
y
)
;
|
(5) |
(C++11 起)
(C++23 起 constexpr) |
|
定义于头文件
<cinttypes>
|
||
|
std
::
imaxdiv_t
div
(
std::
intmax_t
x,
std::
intmax_t
y
)
;
|
(6) |
(C++11 起)
(C++23 起 constexpr) |
|
std
::
imaxdiv_t
imaxdiv
(
std::
intmax_t
x,
std::
intmax_t
y
)
;
|
(7) |
(C++11 起)
(C++23 起 constexpr) |
计算分子 x 除以分母 y 的商和余数。
| (C++11 起) |
|
商为代数商并舍弃任何小数部分(向零截断)。余数满足 quot * y + rem == x 。 |
(C++11 前) |
|
商为表达式 x / y 的结果。余数为表达式 x % y 的结果。 |
(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)]; // 字符串字面量是数组 } 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. | 模运算(及截断除法) —— 摘自维基百科 |