list 的使用

#include <list>
#include <iostream>

using namespace std;

void printList(const list<int>&L){
	for (list<int>::const_iterator it = L.begin(); it != L.end(); it ++){
		cout << *it << ' ';
	}
	cout << endl;
}
void test01(){
	list<int>L;
	
	//尾插
	L.push_back(10);
	L.push_back(20);
	L.push_back(30);
	
	//头插
	L.push_front(100);
	L.push_front(200);
	L.push_front(300);   
	//300 200 100 10 20 30
	printList(L); 
		
	//尾删
	L.pop_back();
	//300 200 100 10 20
	printList(L); 
	
	//头删
	L.pop_front();
	//200 100 10 20
	printList(L);
	
	//insert插入
	//l.insert(l.begin(), 1000)在头部插入1000 
	list<int>::iterator it = L.begin();
	L.insert(++ it, 1000);
	// 加其他的数都是错误形式,坑 :it + 2 是错的 
	//200 1000 100 10 20
	printList(L);
	
	//删除
	it = L.begin();
	L.erase(it);
	//1000 100 10 20
	printList(L); 
	
	//移除
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	L.push_back(10000);
	//1000 100 10 20 10000 10000 10000 10000
	printList(L);
	L.remove(10000);
	//1000 100 10 20
	printList(L); 
 
}

int main(){
	test01();
	
	return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/Iamcookieandyou/p/13169732.html