size_type、size_t、different_type以及ptrdiff_t

  • 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類定義的。
    
/*******************************************
 * this is a simple demo to test size_type
 *
 * Auther : Jerry.Jiang
 * Date : 2011/08/20
 * http://blog.csdn.net/jerryjbiao
 *
 *********************************************/

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string str("This is a simple demo !");

	for (string::size_type index = 0; index != str.size(); ++index)
	{
		cout << str[index];
	}
	cout << endl;

	return 0;
}

      這裏特別注意的是:任何存儲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:

/*******************************************
 * this is a simple demo to test vector::size_type
 *
 * Auther : Jerry.Jiang
 * Date : 2011/08/20
 * http://blog.csdn.net/jerryjbiao
 *
 *********************************************/

#include <iostream>
#include <vector>

using namespace std;

int main()
{
	vector<int> ivec;

	//vector::size_type 
	for (vector<int>::size_type ix = 0 ; ix != 10; ++ix)
	{
		ivec.push_back(ix+1);
	}

	//vector::iterator
	for (vector<int>::iterator iter = ivec.begin();
	                           iter != ivec.end(); ++iter)
	{
		cout << *iter << "  ";
	}

	cout << endl;
	return 0;
}
/*******************************************
 * this is a simple demo to test list::size_type
 *
 * Auther : Jerry.Jiang
 * Date : 2011/08/20
 * http://blog.csdn.net/jerryjbiao
 *
 *********************************************/

#include <list>
#include <iostream>

using namespace std;

int main( )
{

   list <int> c1;
   list <int>::size_type i;
   
   c1.push_back( 1 );
   i = c1.size( );
   cout << "List length is " << i << "." << endl;

   c1.push_back( 2 );
   i = c1.size( );
   cout << "List length is now " << i << "." << endl;

   return 0;
}
/*******************************************
 * this is a simple demo to test map::size_type
 *
 * Auther : Jerry.Jiang
 * Date : 2011/08/20
 * http://blog.csdn.net/jerryjbiao
 *
 *********************************************/

#include <map>
#include <iostream>

int main()
{
    using namespace std;
    map<int, int> m1, m2;
    map<int, int>::size_type i;
    typedef pair<int, int> Int_Pair;

    m1.insert(Int_Pair(1, 1));
    i = m1.size();
    cout << "The map length is " << i << "." << endl;

    m1.insert(Int_Pair(2, 4));
    i = m1.size();
    cout << "The map length is now " << i << "." << endl;
	
	return 0;
}
  • size_t

        size_t類型定義在cstddef頭文件中,該文件是C標準庫中的頭文件 stddef.h 的C++版本。它是一個與機器相關的unsigned類型,其大小足以存儲內存中對象的大小。

         與前面Demo中vector和string中的size操作類似,在標準庫類型bitset中的size操作和count操作的返回值類型爲size_t 。

    /***********************************************
     * this is a simple demo to test bitset::size_t
     *
     * Auther : Jerry.Jiang
     * Date : 2011/08/20
     * http://blog.csdn.net/jerryjbiao
     *
     *********************************************/
    
    #include <iostream>
    #include <bitset>
    
    using namespace std;
    
    int main()
    {
    	//bitvec有32位,每位都是0
    	bitset<32> bitvec;
    	cout << " bitvec : " << bitvec << endl;
    	
    	//count()統計bitvec中置1的個數
    	size_t bitcount = bitvec.count();
    	cout << "bitvec.count() :" << bitcount << endl;
    
    	//size()統計bitvec二進制位的個數
    	size_t bitsize = bitvec.size();
    	cout << "bitvec.size() :" << bitsize << endl;
    
    	return 0;
    }
  • different_type


        一種由vector類型定義的signed整型,用於存儲任意兩個迭代器間的距離。
  • ptrdiff_t


        與size_t一樣,定義在cstddef頭文件中定義的與機器相關的有符號整型,該類型具有足夠的大小存儲兩個指針的差值,這兩個指針指向同一個可能的最大數組。

*******************************************************************************************************************************

C++經典書目索引及資源下載:http://blog.csdn.net/jerryjbiao/article/details/7358796

********************************************************************************************************************************

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章