1. 程式人生 > >C++ STL list的初始化、新增、遍歷、插入、刪除、查詢、排序、釋放

C++ STL list的初始化、新增、遍歷、插入、刪除、查詢、排序、釋放

listC++標準模版庫(STL,Standard Template Library)中的部分內容。實際上,list容器就是一個雙向連結串列,可以高效地進行插入刪除元素。

使用list容器之前必須加上STLlist容器的標頭檔案:#include<list>;

 

list屬於std命名域的內容,因此需要通過命名限定:using std::list;也可以直接使用全域性的名稱空間方式:using namespace std;

 

(1)初始化

typedef struct info_s

{

int nNumber;

 

}info_t;

 

typedef std::list< info_t > list_t;

 

定義list的型別

list_t  List;  //定義一個空的連結串列

list_t  List(count); //建一個含count個預設值是0的元素的連結串列

list_t  List(count, info); //建一個含count個預設值是info的元素的連結串列

list_t  List(List2);   //建一個的copy連結串列

list_t  List(List2.begin(),List2.end());   //含區間的元素

[First,Last]

 

2)新增(新增到末尾)

 

info_t info;

//Set(info)

 

List.push_back(info);

將會新增到末尾

 

(3)遍歷

list_t::iterator iter;

for(iter = List.begin(); iter != List.end() ;iter++)

{

std::cout<< iter->nNumber <<std::endl;

}

 

 

(4)刪除末尾元素

c++stl list 提供pop_back()函式來刪除最後一個元素。

List.pop_back();

 

(5)刪除所有元素

 

簡單粗暴的方法:呼叫clear()

List.clear();

 

遍歷刪除法,一個一個刪除,這樣的好處是,如果元素有申請內容或者系統資源,我們可以把他釋放了,避免資源洩漏。

list_t::iterator iter;

for(iter = List.begin(); iter != List.end() ;)

{

//這裡可以做是否記憶體或者資源的操作

//

iter =  List.erase(iter);  

//iter指向了下一個元素

}

 

 

(6)插入

 

iter = List.insert(iter , info);

 

插入後iter指向新插入的元素。

 

(7)查詢

list_t::iterator iter ;

iter = std::find(List.begin(),List.end(), info);

if(iter != List.end())

{

std::cout<<"find it"<<std::endl;

}

else

{

std::cout<<"not find it"<<std::endl;

}

 

 

注意結構體需要過載==運算子

(8)排序

 

List.sort();

注意,結構體需要過載運算子<

 

 

上程式碼

 

#include <iostream>
#include <list>
#include <algorithm>  

#include <stdlib.h>
#include <string.h>

typedef struct info_s
{
	int nNumber;
	bool operator==(struct info_s b) const  
   {  
	   return this->nNumber == b.nNumber;  
   }  
	
   bool operator!=(struct info_s b) const  
   {  
	   return this->nNumber != b.nNumber;  
   }  
	
   bool operator>=(struct info_s b) const  
   {  
	   return this->nNumber >= b.nNumber;  
   }  
   
   bool operator<=(struct info_s b) const  
   {  
	   return this->nNumber <= b.nNumber;  
   }  
 
	 bool operator>(struct info_s b) const  
	 {	
		 return this->nNumber > b.nNumber;  
	 }	
	 
	 bool operator<(struct info_s b) const  
	 {	
		 return this->nNumber < b.nNumber;  
	 }	

}info_t;

typedef std::list< info_t > list_t;

void append(list_t &List, info_t &info)
{
	std::cout<<"***append****"<<std::endl;
	List.push_back(info);
}

void for_each(list_t &List)
{
	std::cout<<"***for_each****"<<std::endl;
	list_t::iterator iter;
	for(iter = List.begin(); iter != List.end() ;iter++)
	{
		std::cout<< iter->nNumber <<std::endl;
	}
}

void del_end_info(list_t &List)
{
	std::cout<<"***del_end_info****"<<std::endl;
	if(! List.empty())
	{
		List.pop_back();
	}
}

void for_each_delete(list_t &List)
{
	list_t::iterator iter;
	for(iter = List.begin(); iter != List.end() ;)
	{
		std::cout<< "delete before  iter->number:"<<iter->nNumber <<std::endl;
		iter =  List.erase(iter);
		std::cout<< "delete  after iter->number:"<< iter->nNumber <<std::endl;
	}

}


int insert_one(list_t &List , info_t &info, int iPlace)  
{  
    int i = 0;  

	std::cout<<"insert_one"<<std::endl;  
    if(iPlace < 0)  
    {  
        std::cout<<"insert_one  param error"<<std::endl;  
        return -1;  
    }  
  
    list_t::iterator iter = List.begin();
	
    while(iter != List.end())   
    {       
         //std::cout<<" dump "<< (*iVector)<<std::endl;  
         if(i == iPlace)  
         {  
              iter = List.insert(iter , info); //此時insert的返回值是迭代器,插入成功後iVector指向插入的位置  
              std::cout<<" insert_one   after List point "<<iter->nNumber <<std::endl;  
              return 0;  
         }  
           
         i++;  
         ++iter;    
    }    
  
    
    iter = List.insert(List.end() , info);
	
    return 0;  
}  


void find_one(list_t &List,info_t info )
{

	std::cout<<"find_one"<<std::endl;  

	list_t::iterator iter ;

	iter = std::find(List.begin(),List.end(), info);

	if(iter != List.end())
	{
		std::cout<<"find it"<<std::endl; 
	}
	else
	{
		std::cout<<"not find it"<<std::endl; 
	}

}

void Sort(list_t & List)
{
	std::cout<<"Sort it"<<std::endl; 
	List.sort();
	for_each(List);
}

int main()
{
	//初始化
	list_t List;
	info_t info;
	memset(&info, 0, sizeof(info_t));

	//新增
	info.nNumber = 8;
	append(List, info);

	info.nNumber = 5;
	append(List, info);

	info.nNumber = 7;
	append(List, info);
	
	info.nNumber = 1;
	append(List, info);

	info.nNumber = 1;
	append(List, info);

	info.nNumber = 2;
	append(List, info);

	info.nNumber = 1;
	append(List, info);

	//遍歷
	for_each(List);

	//插入
	info.nNumber = 80;
	insert_one(List,info,3);
	for_each(List);
	//查詢
	find_one(List,info);

	//排序
	Sort(List);

	//刪除末尾
	del_end_info(List);
	for_each(List);

	 std::cout<< " size:"<<List.size()<<std::endl; 

	//刪除所有
//	List.clear();
	for_each_delete(List);
	for_each(List);
	std::cout<<  " size:"<<List.size()<<std::endl; 

	
	return 0;
}