时间:2019-05-06 标签:c++uint,unsignedint,int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3552094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
c++ uint , unsigned int , int
提问by Ismail Marmoush
Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering:
嗨,我有一个程序可以处理很多向量和这些向量元素的索引,我想知道:
- is there a difference between
uint
andunsigned int
- which is better to use one of the above types or just use
int
as I read some people say compiler does handle int values more efficiently, but if I usedint
I will have to check always for negative idxs which is pain. - do you think iterators to be better? is it more efficient than normal indexing
vectorx[idx]
?
- 是有之间的差
uint
和unsigned int
- 最好使用上述类型之一,或者只是使用,
int
因为我读到一些人说编译器确实更有效地处理 int 值,但如果我使用,int
我将不得不始终检查负 idxs,这很痛苦。 - 你认为迭代器更好吗?它比普通索引更有效
vectorx[idx]
吗?
p.s the software will handle large data processes and good performance is a must have requirement
ps 该软件将处理大数据处理,良好的性能是必须的
回答by AnT
C++ defines no such type as
uint
. This must be "your" type, i.e. a type defined in your code or some third party library. One can guess that it is the same asunsigned int
. Could beunsigned long int
though or something else. Anyway, you have to check it yourself.It is a matter of personal style. I, for example, believe that one has to use unsigned types to represent naturally non-negative values, like sizes or quantities. There's no difference in performance between signed and unsigned types, aside from some specific contexts. I would say that in most cases it is unsignedtypes that will be handled more efficiently.
Iterators make implementations more generic, i.e. you can use sequential-access iterator and thus make your implementation applicable to any sequential data structure. By using index you impose the random-access requirement on the data structure, which is a strong requirement. It is not a good idea to impose strong requirements when there's no real need for them.
C++ 没有定义
uint
. 这必须是“您的”类型,即在您的代码或某些第三方库中定义的类型。人们可以猜测它与unsigned int
. 可能unsigned long int
虽然或其他什么。无论如何,你必须自己检查。这是个人风格的问题。例如,我认为必须使用无符号类型来表示自然的非负值,例如大小或数量。除了某些特定上下文之外,有符号和无符号类型之间的性能没有区别。我会说在大多数情况下,无符号类型会得到更有效的处理。
迭代器使实现更加通用,即您可以使用顺序访问迭代器,从而使您的实现适用于任何顺序数据结构。通过使用索引,您可以对数据结构强加随机访问要求,这是一个很强的要求。在没有真正需要的情况下强加强要求并不是一个好主意。
回答by TreDubZedd
If you're looping through the vector sequentially, by all means, use the iterator. There is overhead related to indexing, regardless of the index type, which can be avoided by iterating.
如果您按顺序循环遍历向量,请务必使用迭代器。无论索引类型如何,都存在与索引相关的开销,可以通过迭代来避免。
回答by Arslan
1) uint = unsigned int, in fact uint is just a typedeffor unsigned int (will be replaced by unsigned int on compile time).
1) uint = unsigned int,实际上uint只是unsigned int的一个typedef(在编译时会被unsigned int替换)。
2) If you want to add to your code some "security" go with uint, you'll avoid for sure negative values.
2)如果你想在你的代码中添加一些与uint相关的“安全性”,你肯定会避免负值。
3) If you run through the vector sequentially, go with iterators, they are optimized for sequential looping (they are some kind of pointers).
3)如果您按顺序运行向量,请使用迭代器,它们针对顺序循环进行了优化(它们是某种指针)。
回答by HelloDog
As other poster have noted, uint is probably a typedef for unsigned
int
If you're using Visual Studio, you can check that fact very quickly by pressing F12
while the text cursor is in uint
to see its definition.
正如其他海报所指出的, uint 可能是unsigned
int的 typedef如果您使用的是Visual Studio,您可以通过F12
在文本光标所在的同时按下uint
以查看其定义来快速检查该事实。