zoukankan      html  css  js  c++  java
  • size_type、size_t、differentce_type以及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

  • 相关阅读:
    《20170914-构建之法:现代软件工程-阅读笔记》
    《结对-贪吃蛇游戏-开发环境搭建过程》
    《结对-贪吃蛇游戏-设计文档》
    《自我介绍》
    对于软件工程的期望
    GIT的使用方法
    结对-贪吃蛇-需求分析
    团队-井字棋-需求分析
    团队-井字棋-成员简介及分工
    新的目标
  • 原文地址:https://www.cnblogs.com/heyonggang/p/3264422.html
Copyright © 2011-2022 走看看