s*****n 发帖数: 994 | 1 vector v;
v.push_back(1);
for (int i = -1; i < v.size(); i++){
....
}
The code will never run into the for loop..! |
a****2 发帖数: 1458 | 2 why?
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
t****t 发帖数: 6806 | 3 decent compiler will warn you if you enable enough warning options.
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
g**u 发帖数: 504 | 4 I think size_type is unsigned int or unsigned long. Comparing int with
unsigned int will convert int to unsigned automatically, you can convert
unsigned to signed explicitly as the following
int length=(int)v.size();
for (int i = -1; i < length; i++){
...
}
But I cannot see why you want to use it in this way.
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
s******3 发帖数: 7297 | 5 why don't you use iterator??? |
l***i 发帖数: 1309 | 6 C++ is user friendly, it is just picky in choosing friends :) |
m********1 发帖数: 31 | 7 comparison between signed and unsigned integer expressions |
g*********e 发帖数: 14401 | 8 if you use stl, why not use iterator |
w****o 发帖数: 2260 | 9 Interesting articles to talk about size_t, which size_type in STL is defined
as
http://stackoverflow.com/questions/1119370/where-do-i-find-the-
http://eetimes.com/discussion/programming-pointers/4026076/Why-
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
a********m 发帖数: 15480 | 10 这个是比较烦。不过还是有警告的,而且知道了也比较容易理解。
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
|
|
w****o 发帖数: 2260 | 11 就这个话题,想问点儿东西
到底 unsigned int在CPU里面是如何表示的?我觉得通常数据都是存在寄存器(
register)进行运算的。可是寄存器是不区分 signed 还是unsigned的吧?!
我觉得unsigned int这个只是编程语言和编译器弄出来的,不是CPU native support的
,到底编译器是如何区分singed和unsigned的?
是不是要写个代码,到gdb里看汇编来弄明白?!
谢谢!
【在 g**u 的大作中提到】 : I think size_type is unsigned int or unsigned long. Comparing int with : unsigned int will convert int to unsigned automatically, you can convert : unsigned to signed explicitly as the following : int length=(int)v.size(); : for (int i = -1; i < length; i++){ : ... : } : But I cannot see why you want to use it in this way.
|
s*****n 发帖数: 994 | 12 Yes, you are correct. Always use "unsigned int i" when comparing with size()
function.
I am using index instead of iterator is because I have one more situation
which is not any of the vector elements to consider. This is the way to add
it in and save some duplicate lines.
【在 g**u 的大作中提到】 : I think size_type is unsigned int or unsigned long. Comparing int with : unsigned int will convert int to unsigned automatically, you can convert : unsigned to signed explicitly as the following : int length=(int)v.size(); : for (int i = -1; i < length; i++){ : ... : } : But I cannot see why you want to use it in this way.
|
t****t 发帖数: 6806 | 13 quite the contrary, most CPU support unsigned and signed numbers. most
likely they share the binary representation, but instructions distinguish
them. for example, in x86 you have mul and imul for unsigned and signed
integer multiplication. for addition and subtraction, the operations are the
same anyway. but for comparison, CPU do check different flag.
so, the answer is no, most CPU natively support unsigned and signed integers.
【在 w****o 的大作中提到】 : 就这个话题,想问点儿东西 : 到底 unsigned int在CPU里面是如何表示的?我觉得通常数据都是存在寄存器( : register)进行运算的。可是寄存器是不区分 signed 还是unsigned的吧?! : 我觉得unsigned int这个只是编程语言和编译器弄出来的,不是CPU native support的 : ,到底编译器是如何区分singed和unsigned的? : 是不是要写个代码,到gdb里看汇编来弄明白?! : 谢谢!
|
m*********e 发帖数: 55 | 14 signed and unsigned compare应该有warning的
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
c*********t 发帖数: 2921 | 15
【在 t****t 的大作中提到】 : quite the contrary, most CPU support unsigned and signed numbers. most : likely they share the binary representation, but instructions distinguish : them. for example, in x86 you have mul and imul for unsigned and signed : integer multiplication. for addition and subtraction, the operations are the : same anyway. but for comparison, CPU do check different flag. : so, the answer is no, most CPU natively support unsigned and signed integers.
|
c*********t 发帖数: 2921 | 16 学习了。
是不是寄存器本身看不出有无符号?
是否按有符号数操作是由指令决定的?
一个signed和一个unsigned 比较的时候,用的指令同两个signed数作比较时用的指令
相同吗?
【在 t****t 的大作中提到】 : quite the contrary, most CPU support unsigned and signed numbers. most : likely they share the binary representation, but instructions distinguish : them. for example, in x86 you have mul and imul for unsigned and signed : integer multiplication. for addition and subtraction, the operations are the : same anyway. but for comparison, CPU do check different flag. : so, the answer is no, most CPU natively support unsigned and signed integers.
|
y***d 发帖数: 2330 | 17 C/C++ 把各种 size 搞成 unsigned 类型,现在看来这实在是非常脑残的事情;
好处或许是个别程序可以管理更大一点空间,
而坏处就是所有程序都要操心类型转换;那一堆 signed-unsigned 的警告,本质上是
没有意义的事情,却每个人都要花精力在这个上面。
【在 s*****n 的大作中提到】 : vector v; : v.push_back(1); : for (int i = -1; i < v.size(); i++){ : .... : } : The code will never run into the for loop..!
|
y***d 发帖数: 2330 | 18 http://eetimes.com/discussion/programming-pointers/4026076/Why-
"Using unsigned int instead of int as the type of the third parameter lets
memcpy copy larger objects, at no additional cost."
机器是没有什么 cost,可是人力的额外消耗却是无尽的
defined
【在 w****o 的大作中提到】 : Interesting articles to talk about size_t, which size_type in STL is defined : as : http://stackoverflow.com/questions/1119370/where-do-i-find-the- : http://eetimes.com/discussion/programming-pointers/4026076/Why-
|