Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 792
Ismail Marmoush Avatar asked Aug 23 '10 22:08

Ismail Marmoush


People also ask

Is unsigned int same as Uint?

uint isn't a standard type - unsigned int is. and what does this fact implies? That code written with uint won't be inherently portable unless uint is a typedef that you declare actually inside that code.

What is the difference between Uint and int?

uint means “unsigned integer” while int means “signed integer”. Unsigned integers only contain positive numbers (or zero).

What is Uint in C?

uint is a keyword that is used to declare a variable which can store an integral type of value (unsigned integer) from the range of 0 to 4,294,967,295. It keyword is an alias of System.

What is the difference between unsigned int and int in C?

An int is signed by default, meaning it can represent both positive and negative values. An unsigned is an integer that can never be negative. If you take an unsigned 0 and subtract 1 from it, the result wraps around, leaving a very large number (2^32-1 with the typical 32-bit integer size).


2 Answers

  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.

like image 73
AnT Avatar answered Oct 11 '22 10:10

AnT


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.

like image 45
TreDubZedd Avatar answered Oct 11 '22 09:10

TreDubZedd