Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering:
uint
and unsigned int
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.vectorx[idx]
?p.s the software will handle large data processes and good performance is a must have requirement
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.
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.