首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

std::div

Defined in header <cstdlib>

?

?

std::div_t div( int x, int y );

(1)

?

std::ldiv_t div( long x, long y );

(2)

?

std::lldiv_t div( long long x, long long y );

(3)

(since C++11)

std::ldiv_t ldiv( long x, long y );

(4)

?

std::lldiv_t lldiv( long long x, long long y );

(5)

(since C++11)

Defined in header <cinttypes>

?

?

std::imaxdiv_t div( std::intmax_t x, std::intmax_t y );

(6)

(since C++11)

std::imaxdiv_t imaxdiv( std::intmax_t x, std::intmax_t y );

(7)

(since C++11)

计算分子除法的商和馀数。x分母y...

The quotient is the algebraic quotient with any fractional part discarded (truncated towards zero). The remainder is such that quot * y + rem == x.

(until C++11)

The quotient is the result of the expression x/y. The remainder is the result of the expression x%y.

(since C++11)

参数

x, y

-

integer values

返回值

如果余数和商数都可以表示为对应类型%28 int、long、long、std::imaxdiv的对象[医]t,分别为%29,将两者作为类型的对象返回。std::div_t,,,std::ldiv_t,,,std::lldiv_t,,,std::imaxdiv_t定义如下:

性病::DIV[医]T型

二次

代码语言:javascript
复制
struct div_t { int quot; int rem; };

二次

或者。

二次

代码语言:javascript
复制
struct div_t { int rem; int quot; };

二次

性病::[医]T型

二次

代码语言:javascript
复制
struct ldiv_t { long quot; long rem; };

二次

或者。

二次

代码语言:javascript
复制
struct ldiv_t { long rem; long quot; };

二次

STD:lldiv[医]T型

二次

代码语言:javascript
复制
struct lldiv_t { long long quot; long long rem; };

二次

或者。

二次

代码语言:javascript
复制
struct lldiv_t { long long rem; long long quot; };

二次

性病::伊马克斯迪夫[医]T型

二次

代码语言:javascript
复制
struct imaxdiv_t { std::intmax_t quot; std::intmax_t rem; };

二次

或者。

二次

代码语言:javascript
复制
struct imaxdiv_t { std::intmax_t rem; std::intmax_t quot; };

二次

如果不能表示余数或商数,则行为是未定义的。

注记

直到C++11,商数的舍入方向和剩余的符号在内建除法和余数运算符如果操作数中有一个为负值,则实现定义是否为负数,但在std::div...

在许多平台上,一个CPU指令可以同时获得商和剩余部分,这个函数可以利用这一点,尽管编译器通常能够在适当的情况下合并附近的/和%。

二次

代码语言:javascript
复制
#include <string>
#include <cmath>
#include <cstdlib>
#include <iostream>
 
std::string itoa(int n, int base)
{
    std::string buf;
    std::div_t dv{}; dv.quot = n;
    do {
        dv = std::div(dv.quot, base);
        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 << itoa(12345, 10) << '\n'
              << itoa(-12345, 10) << '\n'
              << itoa(65535, 16) << '\n';
}

二次

产出:

二次

代码语言:javascript
复制
12345
-12345
ffff

二次

另见

fmod

remainder of the floating point division operation (function)

remainder (C++11)

signed remainder of the division operation (function)

remquo (C++11)

signed remainder as well as the three last bits of the division operation (function)

c DIV文件

代码语言:txt
复制
 ? cppreference.com

在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com