STL-list詳細

7670 ワード

4.list  
(1)list   
    vector        ,        。

vector   :
	vectoer            ,  vector      ,  
	   (  )   ,             ,     
	         。


vector   :
	                  ,            
	  ,          。


list   
	list        vector   ,        ,     
	                    ,       ,  
	         ,            。


list   :
	  ,list             ,        ,   
	            ,            ,     
	       。



(2)    list		
(1)     
	std::list data;
	
(2)           
	std::list data(10);

	             0。

(3)            
	(1)            
		std::list data(10, 9527);
		10   ,         9527。
		
	(2)            	
    		int buf[] = {1, 2, 4, 56, 23, 45, 3, 55};
    		std::list l(buf, buf+sizeof(buf)/sizeof(buf[0]));



(3)  list     
(1)    list
	(1)  1:     
		list       ,             。
		list              ,         
		begin()   end()    ,         rbegin() 
		rend()    ,    vector 。

		  list       ,  list    []   , 
		     []    ,               
		  ,             。
	
		          ,    *            
		   。

	(2)front() back()  
		front():           。
		back()               

		        vector   front() back()    ,
		    list       ,    at()  。	
		
(2)  
	list  begin() end()      ,      list    。
	       list     ,            list 
	 。
	
	  :
	#include 
	#include
	#include

	template void show_vector(iter first, iter last) {

    		for( ;first!=last; first++) {
            		cout << *first << " ";
    		}
    		cout << endl;
	}
		
	int main(void)
	{
    		int buf[] = {1, 2, 4, 56, 23, 45, 3, 55};

    		std::list l1;
    		std::list l2(10);
    		std::list l3(buf, buf+sizeof(buf)/sizeof(buf[0]));

    		cout << "  l1    " << endl;
    		show_vector(l1.begin(), l1.end());

    		cout << "
l2 " << endl; show_vector(l2.begin(), l2.end()); cout << "
l3 " << endl; show_vector(l3.begin(), l3.end()); return 0; } : l1 l2 0 0 0 0 0 0 0 0 0 0 l3 1 2 4 56 23 45 3 55 : list, , list , 。 list 10 , 0。 list , , 。 (4) list (1) (1)push_front() : list (2)push_back() : list (3)pop_front() : list (4)pop_back() : list (5)insert() : list , 。 (1)interator insert(iterator position, const T& x); x , 。 (2)void insert(iterator position, size_type n, const T& x); n x 。 (3)void insert(iterator position, InputIterator first, InputIterator last); , InputIterator , 。 (6)ereas() : , , 。 (1)iterator erase ( iterator position ); position 。 (2)iterator erase ( iterator first, iterator last ); first last 。 (7)clear() : 。 (8)empty() : list 。 (9)slpice() : list , 。 (1)void splice ( iterator position, list& x ); list splice position 。 (2)void splice ( iterator position, list& x, iterator i ); list i splice position 。 (3)void splice ( iterator position, list& x, iterator first, iterator last ); list [first,last) splice position 。 (10)resize(): list (11)merge(): , 。 void merge ( list& x ); : list , 。 template void merge ( list& x, Compare comp ); : , 。 , merge 。 (2) (1)push_front/push_back/pop_front/popback #include #include #include #include template void show_vector(iter first, iter last) { for( ;first!=last; first++) { cout << *first << " "; } cout << endl; } int main(void) { std::list l; cout << " l 4 int " << endl; l.push_front(1); l.push_back(4); l.push_front(5); l.push_back(3); show_vector(l.begin(), l.end()); cout << "
l 2 int " << endl; l.pop_front(); l.pop_back(); show_vector(l.begin(), l.end()); return 0; } (2)insert/ereas/clear/empty #include #include #include #include template void show_vector(iter first, iter last) { for( ;first!=last; first++) { cout << *first << " "; } cout << endl; } int main(void) { int buf[] = {1, 2, 4, 56, 23, 45, 3, 55}; std::list l; if(l.empty()) cout< #include #include #include template void show_vector(iter first, iter last) { for( ;first!=last; first++) { cout << *first << " "; } cout << endl; } int main(void) { int buf[] = {1, 2, 4, 56, 23, 45, 3, 55}; std::list l1(buf, buf+sizeof(buf)/sizeof(buf[0])); std::list l2; cout << " l1 l2 begin() , l1 " << endl; l2.splice(l2.begin(), l1); cout << "l1 " < #include #include #include #include #include using namespace std; class Student { public: Student(const string name=" ", int num=0):name(name),num(num) { } string getname() const { return name; } int getnum() const { return num; } private: string name; int num; }; template class List { public: typedef std::list Content; typedef typename std::list::iterator Iter; List() { } List(Iter first, Iter last):load(first, last) { } void add_list(T* &stu) {load.push_front(stu);} void add_tail(T* &stu) {load.push_back(stu);} void ereas_head() {load.pop_front();} void ereas_tail() {load.pop_back();} void display() { for(Iter iter=load.begin(); iter!=load.end(); iter++) { cout << (*(iter))->getname() << " " << (*(iter))->getnum() < &list, int count) { string name = "name", newname = ""; char buf[10] = {0}; int num = 0; srand(time(NULL)); for(int i=0; i list; init_stu_list(list, 20); while(1) { cout<> select; switch(select) { case 5: list.display(); break; default: cout << " " << endl; } } return 0; } : / , c++ STL 。