简体   繁体   English

时间:2019-05-06 标签:c++uint,unsignedint,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:嗨,我有一个程序可以处理很多向量和这些向量元素的索引,我想知道:

  1. is there a difference between uint and unsigned int uintunsigned int之间有区别uint
  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.最好使用上述类型之一,或者只使用int因为我读到一些人说编译器确实更有效地处理 int 值,但是如果我使用int我将不得不始终检查负 idxs 这很痛苦。
  3. do you think iterators to be better?你认为迭代器更好吗? is it more efficient than normal indexing vectorx[idx] ?它比普通索引vectorx[idx]更有效吗?

ps the software will handle large data processes and good performance is a must have requirement ps 该软件将处理大数据处理,良好的性能是必须的

  1. C++ defines no such type as uint . C++ 没有定义uint这样的类型。 This must be "your" type, ie a type defined in your code or some third party library.这必须是“您的”类型,即在您的代码或某些第三方库中定义的类型。 One can guess that it is the same as unsigned int .人们可以猜测它与unsigned int相同。 Could be unsigned long int though or something else.虽然可以是unsigned long int或其他东西。 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, ie 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, in fact uint is just a typedef for 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)如果您按顺序运行向量,请使用迭代器,它们针对顺序循环进行了优化(它们是某种指针)。

正如其他海报所指出的那样, uint 可能是unsigned int 的 typedef 如果您使用的是Visual Studio ,您可以通过在文本光标位于uintF12快速检查该事实以查看其定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM