vector.size()和(signed)int [英] vector.size() and (signed) int

查看:95
本文介绍了vector.size()和(signed)int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将(常规)int与std :: vector.size()返回的值

进行比较时,我收到警告,代码类似于以下内容:


int i = // stuff

if(i> = vec.size())

编译器给我一个有符号/无符号不匹配的符号。警告(我正在使用

Visual Studio .NET 2003中的C ++编译器,它告诉我

返回类型是size_t)。将.size()的

返回值分配给int时,我收到类似的警告。然而,即使是在Stroustrup的常见问题上

< http://www.research.att.com/~bs/bs_faq2.html>他有:


for(int i = 0; i< v.size(); ++ i)

在我看来应该是精细。为了安静错误,

我将返回值转换为int:


if(i> = static_cast< int>(vec。尺寸()))

这是不好的风格?有更优雅的解决方案吗?我记得在某个地方读取

,因为隐含的

转换问题可能会突然出现,或者如果值恰好包装,那么应该避免使用无符号的整数

周围(技术上它应该是未定义的,但我认为大多数

实现只会包装值),这就是为什么我会b / b
不想使用它们。例如,在TC ++ PL:SE中,第16.3.4节有一个奇怪问题的例子(但是他说如果我们很幸运,编译器会抱怨

):


由于矢量不能具有负数元素,因此其大小

必须是非负数。这反映在

vector'的size_type必须是无符号类型的要求中。这允许在某些体系结构上具有更大的矢量大小范围。但是,它也可能会导致惊喜:


void f(int i)

{

vector< char> VC0(-1); //很容易让编译器警告

vector< char> vc1(i);

}


void g()

{

f(-1 ); //欺骗f()接受一个大的正数!

}


在调用f(-1)中,-1被转换为a(相当大的)postive

整数(第C.6.3节)。如果我们很幸运,编译器会找到一种方式

的抱怨。

--Bjarne Stroustrup,_The_C ++ _ Programming_Language:_Special_Edition_


-

Marcus Kwok

I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:

int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t). I get similar warnings when assigning the
return value of .size() to an int. However, even on Stroustrup''s FAQ
<http://www.research.att.com/~bs/bs_faq2.html> he has:

for (int i = 0; i<v.size(); ++i)
which seems to me that it should be fine. In order to quiet the error,
I have casted the return value to an int:

if (i >= static_cast<int>(vec.size()))
Is this bad style? Is there a more elegant solution? I recall reading
somewhere that unsigned ints should be avoided due to implicit
conversion issues that may crop up or if the value happens to wrap
around (technically it should be undefined but I assume most
implementations will just wrap the value around), which is why I would
prefer not to use them. For example, in TC++PL:SE, section 16.3.4 has
an example of a weird issue (but he says the compiler will complain if
we are lucky):

Since a vector cannot have a negative number of elements, its size
must be non-negative. This is reflected in the requirement that
vector''s size_type must be an unsigned type. This allows a greater
range of vector sizes on some architectures. However, it can also
lead to surprises:

void f(int i)
{
vector<char> vc0(-1); // fairly easy for compiler to warn against
vector<char> vc1(i);
}

void g()
{
f(-1); // trick f() into accepting a large positive number!
}

In the call f(-1), -1 is converted into a (rather large) postive
integer (Sec. C.6.3). If we are lucky, the compiler will find a way
of complaining.
--Bjarne Stroustrup, _The_C++_Programming_Language:_Special_Edition_

--
Marcus Kwok

推荐答案



" Marcus Kwok" < RI ****** @ gehennom.net>在消息中写道

news:dg ********** @ news-int2.gatech.edu ...

"Marcus Kwok" <ri******@gehennom.net> wrote in message
news:dg**********@news-int2.gatech.edu...
比较时我收到警告(常规)int从std :: vector.size()返回的值,类似于以下代码:

int i = // stuff
if(i> ; = vec.size())

编译器给我一个有符号/无符号不匹配的符号。警告(我正在使用Visual Studio .NET 2003中的C ++编译器,它告诉我
返回类型是size_t)。
I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:

int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t).




任何理由你没有使用unsigned int for i?


(你也可以随意忽略这个警告,如果你知道它永远不会是一个/>
真正的问题。)


-Howard



Any reason you''re not using unsigned int for i?

(You can also feel free to ignore the warning, if you know it''ll never be a
real problem.)

-Howard


Howard写道:
Howard wrote:
在将(常规)int与std :: vector.size()返回的值
进行比较时,我收到警告,代码类似于以下代码:

int i = // stuff
if(i> = vec.size())

编译器给我一个有符号/无符号不匹配的信息。警告(我正在使用Visual Studio .NET 2003中的C ++编译器,它告诉我
返回类型是size_t)。
I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:

int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t).



任何理由你没有为我使用unsigned int?



Any reason you''re not using unsigned int for i?




实际上,我会说他应该默认使用int。在特殊情况下,无符号类型只有

有用。我实际上不明白为什么size_t是

无符号。



Actually, I''d say he should use int by default. unsigned types are only
useful in special cases. I actually don''t understand why size_t is
unsigned.


Rolf Magnus写道:
Rolf Magnus wrote:
Howard写道:

Howard wrote:

将(常规)int与从std返回的值
进行比较时,我收到警告: :vector.size()代码类似如下:

int i = // stuff
if(i> = vec.size())

编译器给我一个有符号/无符号的不匹配。警告(我正在使用Visual Studio .NET 2003中的C ++编译器,它告诉我
返回类型是size_t)。
I am getting warnings when comparing a (regular) int to the value
returned from std::vector.size() in code similar to the following:

int i = //stuff
if (i >= vec.size())
The compiler gives me a "signed/unsigned mismatch" warning (I am using
the C++ compiler found in Visual Studio .NET 2003, which tells me that
the return type is a size_t).



任何理由你没有使用unsigned int for i?



Any reason you''re not using unsigned int for i?



实际上,我会说他应该默认使用int。无符号类型仅在特殊情况下有用。我实际上不明白为什么size_t没有签名。


Actually, I''d say he should use int by default. unsigned types are only
useful in special cases. I actually don''t understand why size_t is
unsigned.




一个人靠我自己的心。我会更进一步说我不知道​​为什么

无符号类型存在。


John



A man after my own heart. I''d go further and say I don''t know why
unsigned types exist.

John


这篇关于vector.size()和(signed)int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆