您好,我有一个程序处理大量向量和这些向量元素的索引,我想知道:
-
uint
和 unsigned int
之间有区别
- 吗?使用上述类型之一或仅使用
int
因为我读到有人说编译器确实更有效地处理 int 值,但如果我使用 int
我将必须始终检查对于负面的 idx 来说,这是痛苦的。
- 你认为迭代器更好吗?它比普通索引
vectorx[idx]
更有效吗?
ps 该软件将处理大数据处理,良好的性能是必须具备的要求
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
and unsigned 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 used int
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 the software will handle large data processes and good performance is a must have requirement
发布评论
评论(4)
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.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.
如果您要按顺序循环访问向量,请务必使用迭代器。无论索引类型如何,都存在与索引相关的开销,可以通过迭代来避免。
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.
1) uint = unsigned int,实际上uint只是unsigned int的typedef(将在编译时被unsigned int替换)。
2)如果您想在代码中添加一些与 uint 相关的“安全性”,那么您肯定会避免使用负值。
3)如果您按顺序运行向量,请使用迭代器,它们针对顺序循环进行了优化(它们是某种指针)。
1) uint = unsigned int, in fact uint is just a typedef for unsigned int (will be replaced by unsigned int on compile time).
2) If you want to add to your code some "security" go with uint, you'll avoid for sure negative values.
3) If you run through the vector sequentially, go with iterators, they are optimized for sequential looping (they are some kind of pointers).
正如其他发帖者所指出的, uint 可能是
unsigned
int 的 typedef如果您使用的是 Visual Studio,则可以在文本光标位于
uint
中时按F12
快速查看其定义。As other poster have noted, uint is probably a typedef for
unsigned
intIf you're using Visual Studio, you can check that fact very quickly by pressing
F12
while the text cursor is inuint
to see its definition.