size_t is the type returned by std::vector.size(), it is an unsigned type. In your code you try to compare int (a signed type) with size_t (unsigned type), that's why you get that compiler warning.
why not use "unsigned" keyword instead of size_t ??
Both are fine to me but I like the unsigned more....
This looping problem could be ignored but sometimes I hate it when it causes hard to be noteciable error
The cases when :
1 2 3 4 5 6 7 8
size_t x1,x2,y1,y2;
x1 = 50; y1 = 20;
x2 = 30; y2 = 100;
// Think that they are 2D Array Indexes
// And I need to find a range between the two
float range = sqrt( pow( x2 - x1, 2 ) + pow( y2-y1, 2 ) );
Casting everytime seems unberable
But remmber when I mention about they being 2D Array indexes
Indexes should always be unsigned ( size_t ) because they are compared with vector.size() which returns unsigned ( size_t )
why not use "unsigned" keyword instead of size_t ??
Because it could be any unsigned type, for example an unsigned int, unsigned long, or even unsigned char. The standard used size_t so the implementation could determine the correct type for their particular operating system, processor.
so tell me about the solution to this problem
Use size_t when comparing to the size of one of the standard containers. By the way this includes the std::string class. This class also returns a size_t when you use the size() function.
unsigned is always unsignedint, but size_t can be any unsigned type.
For example, on a typical today's 64-bit system, unsigned int is 32 bit and size_t is 64 bit. You may not be dealing with >4Gb vectors routinely, but others do, and using an unsigned int to iterate such vector would fail on those systems.
PS: to be pedantic, the return type of std::vector<int>::size() is std::vector<int>::size_type, it just happens to be aliased to std::size_t in all sensible implementations.
I think that your problem is coming from using the variables that you need for your calculations (which should be signed for subtraction operations) also as index values in your for loops. This causes the "coupling" of type requirements you are experiencing.
Use the different types where they are needed.
The only code I see from your posts where a conflict occurs is here:
1 2
vector<int> p = { 90, 30, 50 , 10 };
for( int i = 0; i < p.size(); ++i ) cout << i << ' ';
But I'm guessing your intent here was to display the values of the int types stored in the vector p, so:
1 2
vector<int> p = { 90, 30, 50 , 10 };
for( unsigned i = 0; i < p.size(); ++i ) cout << p[i] << ' ';
should work fine and produce no warnings. In fact, unsigned is more appropriate for the array index in p[i] also.
Please post a fuller of example of where this "type" conflict occurs in your code, and we can take a closer look.
#include <vector>
#include <iostream>
#include <cmath>
usingnamespace std;
int main(){
vector< vector<unsigned> > array( 20, vector<unsigned>( 20, 0 ) );
int hx = 9;
int hy = 9;
unsigned maxrg = 5;
// whether it is size_t, unsigned, unsigned long it is it must be unsigned
// to prevent the warning
for( size_t x = 0; x < array.size(); ++x ){
for( size_t y = 0; y < array[x].size(); ++ y ){
unsigned range = ( unsigned ) sqrt( pow( hx - x, 2 ) + pow( hy - y, 2 ) );
if( range <= maxrg ) {
array[x][y] = range;
}
cout << array[x][y] << ' ';
}
cout << endl;
}
}
Well this causes unexpected result
This part should have casting
unsigned range = ( unsigned ) sqrt( pow( hx - x, 2 ) + pow( hy - y, 2 ) );
sometimes I am just to lazy to scroll from line 1000 to line 100 to find the decleration whether it is signed or unsigned
Use a non-stupid editor that doesn't force you to navigate to a declaration to see it (e.g. that lets see it through tooltips).
It doesn't look like anyone has mentioned this already, so I will. You should ignore this warning and make the cast explicit only if the signed value is guaranteed to be positive. If it's not, insert a check before the comparison to ensure that it is.
Always cast the signed value to unsigned, not the unsigned to signed. For positive values, the former doesn't lose information, while the latter does. Often,
1 < (signed int) 2147483648u
is false, but
(unsigned int) 1 < 2147483648u
is true. One would normally expect that 1 < 2147483648 regardless of hardware details.
If this is windows does not matter if is 32 or 64 bit application, sizeof is always 4.
Why 64 bit will be different and in 32 bit is undefined behaviour ?
ok I will go with the suggestion of using "unsigned" casting
Ehmm, both ways are really is ok
cause I've calculated that my 2D Array won't be anywhere near 2 Billion members
They might be as little as 1024 x 1024
short will do but I don't think short is a good idea
The non-stupid IDE Ehmm I am using Notepad++ and Code::blocks and I am not thinking on moving to anything. My computer is not really that great. Sometimes opening large files such as sqlite causes lag.
Any suggestion for notepad++ plugin or a another non-stupid lightweight IDE ?
for ( std::vector<int>::size_type i = 0; i < v.size(); i++ ) std::cout << v[i] << ' ';
It is not necessary that size_t is equivalent to std::vector<int>::size_type. In fact according to the C++ standard the type that can be used to traverse a vector can be made as