如何在c++中将div_t操作转换为int
how to make a div_t operation into an int in c++
可能很简单,但我试图在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
相关文章:
- 逐位操作的隐式类型转换
- C++:如何将 std::vector 操作转换为 Eigen::VectorXf?
- 使用用户定义的操作隐式转换为类型
- 复制交换习惯用法-我们可以在这里使用动态强制转换操作吗
- 自动符号转换,用于对类等数学向量进行操作
- 位操作将最左侧的设置位转换为右侧交替位?
- 将 int 转换为双精度以执行 std::complex <double>的"*"操作
- CAN模板类型推理考虑原始操作类型转换
- 将 lua 脚本转换为等效于它的指令(操作码)
- C 库中是否有任何功能将字符串转换为算术操作
- 为什么C++标准指定有符号整数在具有混合符号的二进制操作中强制转换为无符号整数
- 是否可以将结构与条件语句完全相同的字符串转换为可以读取和操作的语句?
- 无法通过此操作,因为类型的错误转换
- 操作方法:将 boost::endian 缓冲区类型转换回本机格式
- 如何将字节操作转换为C#
- 将字符串数值操作转换为实际整数操作
- 如何将std::array的c++ 11操作转换为Boost+VS08
- 用简单的操作转换子类向量时得到错误
- 如何在c++中将div_t操作转换为int
- 如何使用位操作转换大小写