c++ uint 、无符号整型、整型

发布于 2024-09-16 07:47:48 字数 325 浏览 2 评论 0原文

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

  1. uintunsigned int 之间有区别
  2. 吗?使用上述类型之一或仅使用 int 因为我读到有人说编译器确实更有效地处理 int 值,但如果我使用 int 我将必须始终检查对于负面的 idx 来说,这是痛苦的。
  3. 你认为迭代器更好吗?它比普通索引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:

  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 the software will handle large data processes and good performance is a must have requirement

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

血之狂魔 2024-09-23 07:47:48
  1. C++ 没有定义像 uint 这样的类型。这必须是“您的”类型,即您的代码或某些第三方库中定义的类型。可以猜到它与unsigned int相同。可能是unsigned long int或者其他。不管怎样,你得自己检查一下。

  2. 这是个人风格的问题。例如,我认为必须使用无符号类型来表示自然非负值,例如大小或数量。除了一些特定的上下文之外,有符号类型和无符号类型之间的性能没有差异。我想说,在大多数情况下,无符号类型的处理效率会更高。

  3. 迭代器使实现更加通用,即您可以使用顺序访问迭代器,从而使您的实现适用于任何顺序数据结构。通过使用索引,您对数据结构施加了随机访问要求,这是一个强烈的要求。当没有真正需要时强加强要求并不是一个好主意。

  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.

  2. 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.

  3. 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.

伊面 2024-09-23 07:47:48

如果您要按顺序循环访问向量,请务必使用迭代器。无论索引类型如何,都存在与索引相关的开销,可以通过迭代来避免。

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.

离线来电— 2024-09-23 07:47:48

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).

ぺ禁宫浮华殁 2024-09-23 07:47:48

正如其他发帖者所指出的, uint 可能是 unsigned int 的 typedef
如果您使用的是 Visual Studio,则可以在文本光标位于 uint 中时按 F12 快速查看其定义。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文