码迷,mamicode.com
首页 > 其他好文 > 详细

v.size() return size_t not int 返回无符号整型数

时间:2015-05-08 06:59:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

 

In the C++ STL, the vector size() function return size_t, which is unsigned int, not int. So imagine this, define an empty vector<int> v, and v.size() should be 0, and you have a for loop, and has an end condition is v.size() - 1, which is 4294967295 actually, not -1. This might cause problem when i = 0, which can enter the for loop, but actually was supposed not entering it.

 

vector<int> v;
cout << v.size() << endl;      // 0
cout << v.size() - 1 << endl;  // 4294967295 
cout << v.size() - 2 << endl;  // 4294967294

cout << v.size() << endl;           // 0
cout << int(v.size() - 1) << endl;  // -1 
cout << int(v.size() - 2) << endl;  // -2

 

v.size() return size_t not int 返回无符号整型数

标签:

原文地址:http://www.cnblogs.com/grandyang/p/4486638.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!