list類的使用與泛型編程

#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <list>

//使用模板的原因:類型不同,操作相同,就可以封裝成模板
//體現泛型編程的動機--通用
template< class T >//此處class也可以被typename代替
class MyList : public std::list< T >
{
public:
	
    //重載+=,插入某條鏈表的末尾,表示鏈接
	MyList& operator += ( const MyList& rcMyList) { if( ! rcMyList.empty() ) { insert( this->end(), rcMyList.begin(), rcMyList.end());} return *this; } // leszek
    T popBack()                           { T cT = this->back(); this->pop_back(); return cT;  }
    T popFront()                          { T cT = this->front(); this->pop_front(); return cT; }
    void pushBack(const  T& rcT )     //&表引用    
	{ 
		if( sizeof(T) == sizeof(void*)) 
		{ 
			if( rcT != NULL )
			{ 
				push_back( rcT );
			} 
		} 
	} // Fix crash with Linux 64 systems. -Dong
    void pushFront( const T& rcT )        
	{ 
		if( sizeof(T) == sizeof(void*)) 
		{ 
			if( rcT != NULL )
			{
			   	push_front( rcT);
			}
	   	} 
	}
    typename std::list<T>::iterator  find( const T& rcT ) {  return std::find( this->begin(), this->end(), rcT ); } // leszek,使用了遍歷
};

typedef struct PicBuffer_
{
	std::string strPic;
	int count;
}PicBuffer;

typedef MyList< PicBuffer* > PicBufferList;
//typedef typename int Int;//typename只能在模板裏使用

int main()
{
   std::string str1("hello world");
   str1 = "hello world!";
   int n = str1.find("hello");   //n = 0
   int m = str1.find("hello", 5);//m = -1
   int k = str1.find("world", 6);//k = 6
   PicBuffer tPicBuffer1;
   //memset(&tPicBuffer1, 0x00, sizeof(tPicBuffer1));//含有string不能memset,會core down,可能是把string變量的地址置爲0x00
   tPicBuffer1.strPic = "hello";//象這樣初始化
   tPicBuffer1.count = 0;
   PicBuffer tPicBuffer2;
   //memset(&tPicBuffer2, 0x00, sizeof(tPicBuffer2));
   tPicBuffer2.strPic = "world";
   tPicBuffer2.count = 1;
   
   PicBufferList tPicBufferList;

   //下面四個只能按順序彈回給自己,否則會出現段錯誤
   tPicBufferList.pushBack(&tPicBuffer1);//&表取地址
   tPicBufferList.pushBack(&tPicBuffer2);
   tPicBufferList += tPicBufferList; 
   memcpy(&tPicBuffer2, tPicBufferList.popBack(), sizeof(tPicBuffer2));
   memcpy(&tPicBuffer1, tPicBufferList.popBack(), sizeof(tPicBuffer1)); 

   //tPicBufferList.find(&tPicBuffer1);//此處編譯不過去!!!(*)
   /*
   if( NULL == tPicBufferList.find(&tPicBuffer1))
   {
	   printf("Not Find!");
   }
   */
   //memcpy與snprintf的區別:memcpy可以拷貝任何類型相同的數據,因爲它使用的是泛型void*,而snpintf只能使用char*
   //而snprintf的好處是能夠拷貝的字符串長度
   //在window上是_snprintf
   return 0;
}

//上述typename的使用稱爲嵌套依賴類型名,如果去掉typename,則不是合法的 C++,不會被編譯器識別,但此處是例外
//告訴 C++ C::const_iterator 是一個 type(類型)取得一個 container(容器)和這個 container
//(容器)中的一個 iterator(迭代器)的 function template(函數模板)


//如果你覺得讀 std::iterator_traits<IterT>::value_type 令人討厭,就想象那個與它相同的東西來代表它。
//所以上述代碼可以寫成 
//typedef std::list<T>::iterator MyIterator;
//MyIterator find( const T& rcT ) {  return std::find( this->begin(), this->end(), rcT ); }
//參考鏈接:http://dev.yesky.com/13/2221013.shtml
對於模板,不同的參數實例化出不同的重載函數,所以在(*)會如下報錯:
main.cpp: In member function ‘typename std::list<T, std::allocator<_CharT> >::iterator MyList<T>::find(const T&) [with T = PicBuffer*]’:
main.cpp:75:   instantiated from here
main.cpp:38: error: no matching function for call to ‘find(std::_List_iterator<PicBuffer*>, std::_List_iterator<PicBuffer*>, PicBuffer* conX
即編譯只對真正實例化時,纔會去編譯出實例的機器代碼。沒調用到相關的成員函數,就不會進行嚴格的類型檢查!編譯器不會報錯。

因爲template沒有強的檢查機制,因此寫出來的模板,必須實例化才能驗證。

另外外C++使用模板的缺點是導致程序程序編譯速度極慢無比,生成的執行程序尺寸超常。

相關鏈接:http://blog.csdn.net/whinah/article/details/2315356 關鍵是要討論如何高效利用  揚長避短等 

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