什么时候使用uint? DIS /优势? [英] when to use uint? dis/advantages?
问题描述
只是想知道使用uint vs int有什么不同之处。当
将是使用它的最佳时间?
我个人并不是那么多,但我喜欢优化我的代码
并使其尽可能有效。所以我觉得使用一个int
只需要一个uint是浪费。
例如类似于(int i = 0; i< 100; i ++)
可能最好写成(uint i = 0; i< 100; i ++)
或者甚至更好(unsigned short i = 0; i< 100; i ++)
使用uint的一大优势是,如果我有一个像
这样的函数/>
void foobar(uint i)
{
if(i> = 0){
cout<< ; 产生警告,这总是正确的 <<结束;
}
}
并且调用foobar(-1)会产生警告。这可以帮助我在编译时解决一些问题/错误。
但是使用uint确实需要更多工作,例如在比较
uint to int会产生一个警告,因此需要一个static_cast。
无论如何,我想知道其他程序员在做什么。我在工作中使用Qt很多
,而且我在Qt的代码中看不到很多东西。这可能是为了
便携性的原因吗?
无论如何,所有评论都是适用的。 TIA。
歌
Hi,
Just wondering what are the dis/advantages of using uint vs int. When
would be the best time to use it?
Personally I don''t use uint that much, but I like to optimize my code
and make it as effective as possible. So I feel that using an int where
only an uint is needed is a waste.
e.g. something like (int i = 0; i < 100; i++)
could probably better be written as (uint i = 0; i < 100; i++)
or maybe even better (unsigned short i = 0; i < 100; i++)
One big advantage of using uint is that if I have a function like
void foobar(uint i)
{
if (i >= 0) {
cout << "generate a warning, this is always true" << endl;
}
}
and calling foobar(-1) will generate a warning. This can help me iron
out some problems/bugs at compile time.
However using uint does require more work, for example when comparing an
uint to int will generate a warning, hence would require a static_cast.
Anyway, I want to know what other programmers are doing. I use Qt a lot
at work, and I don''t see a lot of uint in Qt''s code. Could this be for
portability reasons?
Anyway all comments are appricated. TIA.
Song
推荐答案
宋云昭写道:
你好,
只是想知道使用uint vs int有什么不同之处。
Hi,
Just wondering what are the dis/advantages of using uint vs int.
什么是uint?
Jonathan
What''s uint?
Jonathan
Jonathan Turkanis写道:
Jonathan Turkanis wrote:
什么是uint?
< Jonathan
What''s uint?
Jonathan
unsigned int
unsigned int
Song Yun Zhao写道:
Song Yun Zhao wrote:
Jonathan Turkanis写道:
Jonathan Turkanis wrote:
什么是uint?
unsigned int
unsigned int
或者它可能是:
typedef mounatin_range< uinta> uint;
Jonathan
Or it could be:
typedef mounatin_range<uinta> uint;
Jonathan
这篇关于什么时候使用uint? DIS /优势?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!