小编ten*_*dim的帖子

C++ vector :: size_type:signed vs unsigned; int与long

我一直在通过在不同平台上编译来对我的应用程序进行一些测试,而从64位系统向32位系统的转变暴露了许多问题.

我大量使用向量,字符串等,因此需要对它们进行计数.但是,我的函数也使用32位无符号数,因为在很多情况下我需要显式地使用正整数.

我遇到了看似简单的任务的问题,例如std::minstd::max,这可能更系统化.请考虑以下代码:

uint32_t getmax()
{
    return _vecContainer.size();
}
Run Code Online (Sandbox Code Playgroud)

看起来很简单:我知道向量不能有负数的元素,所以返回无符号整数就完全有道理了.

void setRowCol(const uint32_t &r_row; const uint32_t &r_col)
{
    myContainer_t mc;
    mc.row = r_row;
    mc.col = r_col;
    _vecContainer.push_back(mc);
}
Run Code Online (Sandbox Code Playgroud)

再次,简单.

问题:

uint32_t foo(const uint32_t &r_row)
{
    return std::min(r_row, _vecContainer.size());
}
Run Code Online (Sandbox Code Playgroud)

这给了我错误,例如:

/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../include/c++/v1/algorithm:2589:1: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('unsigned long' vs. 'unsigned int')
min(const _Tp& __a, const _Tp& __b)
Run Code Online (Sandbox Code Playgroud)

我做了很多挖掘,在一个平台上,vector :: size_type是一个8字节的数字.但是,根据设计,我使用无符号的4字节数字.这可能会导致事情变得古怪,因为您无法隐式地将8字节数转换为4字节数.

解决方案是做老式的weay:

#define MIN_M(a,b) a < b ? a : b
return …
Run Code Online (Sandbox Code Playgroud)

c++ vector multiplatform long-integer

6
推荐指数
2
解决办法
1759
查看次数

标签 统计

c++ ×1

long-integer ×1

multiplatform ×1

vector ×1