时间:2019-05-10 标签:c++uint,unsignedint,int

2022-01-10 00:00:00 indexing int iterator unsigned c++

您好,我有一个程序处理大量向量和这些向量元素的索引,我想知道:

Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering:

  1. uintunsigned int有区别吗
  2. 最好使用上述类型之一或仅使用 int 因为我读到有人说编译器确实更有效地处理 int 值,但如果我使用 int我必须始终检查是否有负 idxs,这很痛苦.
  3. 您认为迭代器更好吗?它比普通索引 vectorx[idx] 更有效吗?
  1. is there a difference between uint and unsigned int
  2. 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.
  3. 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

推荐答案

  1. C++ 没有定义像 uint 这样的类型.这必须是您的"类型,即在您的代码或某个第三方库中定义的类型.可以猜到它与unsigned int 相同.可能是 unsigned long int 或其他东西.无论如何,你必须自己检查.

  1. 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 as unsigned int. Could be unsigned 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.

相关文章