size_type、size_t、differentce_type以及ptrdiff_t

来源:互联网 发布:淘宝信用值怎么增加 编辑:程序博客网 时间:2024/05/01 21:13

size_type、size_t、differentce_type以及ptrdiff_t

 

目录(?)[-]

  1. size_type
  2. size_t
  3. different_type
  4. ptrdiff_t

 

 

 

 

复制代码
size_t是unsigned类型,用于指明数组长度或下标,它必须是一个正数,std::size_tptrdiff_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

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 刚种的花蔫了怎么办 鲜切花花朵蔫了怎么办 兔子扭伤脚肿了怎么办 兔子的耳朵肿了怎么办 家里养兔子大了怎么办 幼兔不吃兔粮怎么办 大兔子咬小兔子怎么办 买的小兔子拉稀怎么办 半个月的小兔子怎么办 母兔下崽没奶怎么办 母松鼠下崽后没有奶怎么办 母猫下崽后小猫没奶吃怎么办 母兔产后没奶水怎么办 兔子生崽了不管怎么办 兔子下小兔不管小兔怎么办 兔子下小兔示喂奶怎么办 兔子生完小兔不喂奶怎么办 小兔子生宝宝了怎么办 人摸了小兔崽怎么办 狗狗尿道有脓怎么办 笼养母兔下崽了怎么办 小羊羔站不起来怎么办 兔子不让小兔子吃奶怎么办 兔子不吃东西没精神怎么办 母兔没有初奶怎么办 兔子只喝水不吃东西怎么办 兔子不吃东西也不喝水怎么办 兔子怀孕后不爱吃东西喝水怎么办 母兔产仔无奶怎么办 仔兔十五天母兔没奶怎么办 兔子刚生下兔宝宝该怎么办 兔子不吃草超瘦怎么办 兔子喝水喝多了怎么办 狗吃了变质食物怎么办 狗崽20天没睁眼怎么办 刚生的小狗缺氧怎么办 狗狗生出来了怎么办 刚生的小狗狗死了怎么办 母狗生的死狗怎么办 狗生宝宝都死了奶水怎么办 狗狗生出来不动怎么办