如何在c++中将div_t操作转换为int

how to make a div_t operation into an int in c++

本文关键字:操作 转换 int div c++ 中将      更新时间:2023-10-16

可能很简单,但我试图在c++中获得余数。我试图使用div_t并执行该操作以获得余数。这是我正在使用的代码。

div_t rb;
    rb = div(xx, 8);
    int rgtBbox = rb;

实际上我只需要知道如何将rb转换成int型。我从visual studio返回的错误是没有从div_t到int的转换函数。

只是为了了解更多信息,这里是整个函数

bool Game::grid_place_meeting(int x, int y)
{
    int xx = x;
    int yy = y;
    //The players 'map' position
    div_t rb;
    rb = div(xx, 8);
    int rgtBbox = rb;
    div_t leftBbox;
    leftBbox = div(xx+8, 8);
    div_t botBbox;
    botBbox = div((yy), 8);
    div_t topBbox;
    topBbox = div((yy + 8), 8);

    //check for x_Meeting
    bool x_meeting = false;
    bool y_meeting = false;
    if( (map[rgtBbox][topBbox] != floor) || (map[leftBbox][topBbox]) != floor)
    {
        x_meeting = true;
    }
    if ((map[rgtBbox][botBbox] != floor) || (map[rgtBbox][botBbox]) != floor)
    {
        y_meeting = true;
    }
    return x_meeting || y_meeting;
}

div将提供余数,但它也提供商。既然有两个数字,那么从int rgtBbox = div(xx, 8)中期望哪一个是合理的?

div_t通过将余商捆绑到一个可返回对象中来解决这个问题。根据有信誉的文档,div_t定义为:

struct div_t 
{ 
    int quot; 
    int rem; 
};

所以需要的是访问rem

int rgtBbox = rb.rem;

或者使用模运算符:

int rgtBbox = xx % 8