C ++ uint,unsigned int,int [英] c++ uint , unsigned int , int
问题描述
我有一个程序,它处理向量和这些向量元素的索引,我想知道:
Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering:
-
uint
和unsigned int
之间有区别吗
- 最好使用上述类型之一,或者仅使用
int
,因为我读到一些人说编译器确实可以更有效地处理int值,但是如果使用int
,我将必须始终检查负IDx是痛苦. - 您认为迭代器会更好吗?比普通索引
vectorx[idx]
更有效?
- 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]
?
p.s该软件将处理大数据流程,并且必须具备良好的性能
p.s the software will handle large data processes and good performance is a must have requirement
推荐答案
-
C ++没有将
uint
定义为此类.这必须是您的"类型,即在您的代码或某些第三方库中定义的类型.可以猜到它与unsigned int
相同.可能是unsigned long int
或其他名称.无论如何,您必须自己检查一下.
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.
这是个人风格的问题.例如,我认为必须使用无符号类型来表示自然的非负值,例如大小或数量.除了某些特定的上下文外,有符号和无符号类型在性能上没有区别.我会说,在大多数情况下, unsigned 类型会更有效地处理.
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 unsigned types 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,int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!