std::div, std::ldiv, std::lldiv, std::imaxdiv
来自cppreference.com
在标头 <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(longlong x, longlong y ); | (3) | (C++11 起) (C++23 起为 constexpr ) |
std::ldiv_t ldiv(long x, long y ); | (4) | (C++23 起为 constexpr ) |
std::lldiv_t lldiv(longlong x, longlong 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 的商和余数。
只有在 std::intmax_t 是扩展整数类型时,<cinttypes> 中才会提供 | (C++11 起) |
商是舍弃任何小数部分(向零截断)的代数商。余数满足 quot * y + rem == x。 | (C++11 前) |
商是表达式 x / y 的结果。余数是表达式 x % y 的结果。 | (C++11 起) |
目录 |
[编辑]参数
x, y | - | 整数值 |
[编辑]返回值
如果余数和商都能表示成对应类型(分别是 int、long、longlong、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 {longlong quot;longlong rem;};
或
struct lldiv_t {longlong rem;longlong 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 问题 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) | 除法运算的有符号余数和最后三个二进制位 (函数) |
div 的 C 文档 |
[编辑]外部链接
1. | Euclidean division — Wikipedia |
2. | Modulo (and Truncated division) — Wikipedia |