size_type、size_t、differentce_type以及ptrdiff_t

简介: 目录(?)[-] size_type size_t different_type ptrdiff_t         size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.
 

 

 

 

 

size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_t

ptrdiff_t是signed类型,用于存放同一数组中两个指针之间的差距,它可以使负数,std::ptrdiff_t.

size_type是unsigned类型,表示容器中元素长度或者下标,vector<int>::size_type i = 0;

difference_type是signed类型,表示迭代器差距,vector<int>:: difference_type = iter1-iter2.

前二者位于标准类库std内,后二者专为STL对象所拥有。

 

 

size_type

    在标准库string类型中,最容易令人产生误解就是size()成员函数的返回值了,如果不深入分析的话,大多人都会认为size()的返回值为int类型,其实不然。事实上,size操作返回的是string::size_type类型的值。 那怎样理解size_type这一类型呢,我引用《C++ Primer》一段原文简单解释一下:
    string类类型和许多其他库类型都定义了一些配套类型(companion type)。通过这些配套类型,库类型的使用就能和机器无关(machine-independent)。size_type就是这些配套类型中的一种。它定义为与unsigned型(unsigned int 或 unsigned long)具有相同的含义,而且可以保证足够大能够存储任意string对象的长度。为了使用由string类型定义的size_type类型,程序员必须加上作用域操作符来说明所使用的size_type类型是由string类定义的。

 

  1. /******************************************* 
  2.  * this is a simple demo to test size_type 
  3.  * 
  4.  * Auther : Jerry.Jiang 
  5.  * Date : 2011/08/20 
  6.  * http://blog.csdn.net/jerryjbiao 
  7.  * 
  8.  *********************************************/  
  9.   
  10. #include <iostream>  
  11. #include <string>  
  12.   
  13. using namespace std;  
  14.   
  15. int main()  
  16. {  
  17.     string str("This is a simple demo !");  
  18.   
  19.     for (string::size_type index = 0; index != str.size(); ++index)  
  20.     {  
  21.         cout << str[index];  
  22.     }  
  23.     cout << endl;  
  24.   
  25.     return 0;  
  26. }  

      这里特别注意的是:任何存储string的size操作结果的变量必须为string::size_type类型,同时,使用size_type类型时,必须指出该类型是在哪里定义的。切记不要吧size的返回值赋给一个int变量。

     不仅string类型定义了size_type,其他标准库类型如vector::size_type,list::size_typedeque::size_type,map::size_typemultimap::size_typebasic_string::size_type 等更多请查看MSDN详细介绍。下面是几个常用的Demo:

  1. /******************************************* 
  2.  * this is a simple demo to test vector::size_type 
  3.  * 
  4.  * Auther : Jerry.Jiang 
  5.  * Date : 2011/08/20 
  6.  * http://blog.csdn.net/jerryjbiao 
  7.  * 
  8.  *********************************************/  
  9.   
  10. #include <iostream>  
  11. #include <vector>  
  12.   
  13. using namespace std;  
  14.   
  15. int main()  
  16. {  
  17.     vector<int> ivec;  
  18.   
  19.     //vector::size_type   
  20.     for (vector<int>::size_type ix = 0 ; ix != 10; ++ix)  
  21.     {  
  22.         ivec.push_back(ix+1);  
  23.     }  
  24.   
  25.     //vector::iterator  
  26.     for (vector<int>::iterator iter = ivec.begin();  
  27.                                iter != ivec.end(); ++iter)  
  28.     {  
  29.         cout << *iter << "  ";  
  30.     }  
  31.   
  32.     cout << endl;  
  33.     return 0;  
  34. }  
  1. /******************************************* 
  2.  * this is a simple demo to test list::size_type 
  3.  * 
  4.  * Auther : Jerry.Jiang 
  5.  * Date : 2011/08/20 
  6.  * http://blog.csdn.net/jerryjbiao 
  7.  * 
  8.  *********************************************/  
  9.   
  10. #include <list>  
  11. #include <iostream>  
  12.   
  13. using namespace std;  
  14.   
  15. int main( )  
  16. {  
  17.   
  18.    list <int> c1;  
  19.    list <int>::size_type i;  
  20.      
  21.    c1.push_back( 1 );  
  22.    i = c1.size( );  
  23.    cout << "List length is " << i << "." << endl;  
  24.   
  25.    c1.push_back( 2 );  
  26.    i = c1.size( );  
  27.    cout << "List length is now " << i << "." << endl;  
  28.   
  29.    return 0;  
  30. }  
  1. /******************************************* 
  2.  * this is a simple demo to test map::size_type 
  3.  * 
  4.  * Auther : Jerry.Jiang 
  5.  * Date : 2011/08/20 
  6.  * http://blog.csdn.net/jerryjbiao 
  7.  * 
  8.  *********************************************/  
  9.   
  10. #include <map>  
  11. #include <iostream>  
  12.   
  13. int main()  
  14. {  
  15.     using namespace std;  
  16.     map<intint> m1, m2;  
  17.     map<intint>::size_type i;  
  18.     typedef pair<intint> Int_Pair;  
  19.   
  20.     m1.insert(Int_Pair(1, 1));  
  21.     i = m1.size();  
  22.     cout << "The map length is " << i << "." << endl;  
  23.   
  24.     m1.insert(Int_Pair(2, 4));  
  25.     i = m1.size();  
  26.     cout << "The map length is now " << i << "." << endl;  
  27.       
  28.     return 0;  
  29. }  
    • size_t

          size_t类型定义在cstddef头文件中,该文件是C标准库中的头文件 stddef.h 的C++版本。它是一个与机器相关的unsigned类型,其大小足以存储内存中对象的大小。

           与前面Demo中vector和string中的size操作类似,在标准库类型bitset中的size操作和count操作的返回值类型为size_t

      1. /*********************************************** 
      2.  * this is a simple demo to test bitset::size_t 
      3.  * 
      4.  * Auther : Jerry.Jiang 
      5.  * Date : 2011/08/20 
      6.  * http://blog.csdn.net/jerryjbiao 
      7.  * 
      8.  *********************************************/  
      9.   
      10. #include <iostream>  
      11. #include <bitset>  
      12.   
      13. using namespace std;  
      14.   
      15. int main()  
      16. {  
      17.     //bitvec有32位,每位都是0  
      18.     bitset<32> bitvec;  
      19.     cout << " bitvec : " << bitvec << endl;  
      20.       
      21.     //count()统计bitvec中置1的个数  
      22.     size_t bitcount = bitvec.count();  
      23.     cout << "bitvec.count() :" << bitcount << endl;  
      24.   
      25.     //size()统计bitvec二进制位的个数  
      26.     size_t bitsize = bitvec.size();  
      27.     cout << "bitvec.size() :" << bitsize << endl;  
      28.   
      29.     return 0;  
      30. }  
    • differentce_type


          一种由vector类型定义的signed整型,用于存储任意两个迭代器间的距离
    • ptrdiff_t


          与size_t一样,定义在cstddef头文件中定义的与机器相关的有符号整型,该类型具有足够的大小存储两个指针的差值,这两个指针指向同一个可能的最大数组。

      来源:http://blog.csdn.net/jerryjbiao/article/details/6705331

img_e00999465d1c2c1b02df587a3ec9c13d.jpg
微信公众号: 猿人谷
如果您认为阅读这篇博客让您有些收获,不妨点击一下右下角的【推荐】
如果您希望与我交流互动,欢迎关注微信公众号
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。

目录
相关文章
|
3月前
【Simulink】报错:Size mismatch (size [2 x 1] ~= size [1 x 1]). The size to the left is the size of the l
【Simulink】报错:Size mismatch (size [2 x 1] ~= size [1 x 1]). The size to the left is the size of the l
|
关系型数据库 MySQL 数据库
MySQL新增字段报错:ERROR 1118 -- Row size too large. The maximum row size for the used table type
MySQL新增字段报错:ERROR 1118 -- Row size too large. The maximum row size for the used table type
1465 0
|
6月前
|
Java
【ES异常】mapper [sortNum] of different type, current_type [long], merged_type [keyword]
【ES异常】mapper [sortNum] of different type, current_type [long], merged_type [keyword]
49 0
|
4月前
|
关系型数据库 MySQL
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
MySQL【问题 02】报错 1709 - Index column size too large. The maximum column size is 767 bytes. 可能是最简单的方法
53 0
|
8月前
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
YOLO V5出现RuntimeError: result type Float can‘t be cast to the desired output type long int解决方法
489 0
|
8月前
|
索引
Result window is too large, from + size must be less than or equal to: [10000]
Result window is too large, from + size must be less than or equal to: [10000]
65 0
|
程序员 算法框架/工具 Caffe
解决办法:error: 'size_t' does not name a type、unknown type name 'size_t'
解决办法:error: 'size_t' does not name a type、unknown type name 'size_t'
584 0
【TP5.1】variable type error: array
【TP5.1】variable type error: array
146 0
【TP5.1】variable type error: array