STL : list

#include <iostream>
#include <list>
using namespace std;

bool f(int a) {
	return a > 5;
}

int main(int argc, char **argv) 
{
	
	list<int> a(10, 1);
	list<int> b(a);
	list<int> c = { 5,10,15,20,25 };
	
	/**********************查找屬性********************/
	a.empty();                           // 空,返回true
	list<int>::size_type sz = a.size();  // 元素個數
	a.resize(3, 2);                      // 調整大小,多刪少補,值可指定否則隨機
	int first = a.front();               // 返回頂部元素
	int last = a.back();                 // 返回尾部元素

	/***********************增加元素*******************/
	a.push_front(3);                     // 頭插
	a.push_back(4);                      // 尾插
	list<int>::iterator it1 = a.begin(); 
	list<int>::iterator it2 = a.end();
	advance(it1, 3);                     // 迭代器指針前移3次
	a.insert(it1, 5);                    //在迭代器位置插入5
	a.insert(it1,3,5);                   //在迭代器位置插入3個5
	
	/***********************刪除元素*******************/
	a.pop_back();         
	a.pop_front();
	a.erase(it1);                        // 刪除單個元素
	a.erase(it1,it2);                    // 刪除之間的所有元素
	a.clear();                           // size爲0,存儲空間不變
	a.remove_if(f);                      // 刪除使f返回true的結點
	a.unique();                          // 從有序鏈表刪除重複元素 a.unique(f)

	///***********************修改元素*******************/
	b.assign(a.begin(), a.end()); // 拷貝a
	b.assign(6, 7);               // 刪除所有結點並對b重新賦值
	a.swap(b);                    // 交換兩鏈表
	a.merge(b);                   // 合併鏈表(a有序,b無序,合併成有序)
	a.splice(a.begin(), b);       // 合併鏈表(無序)
	a.reverse();                  // 反轉鏈表
	a.sort();                     // 升序鏈表
	
	///***********************打印鏈表*******************/
	for (list<int>::iterator it1 = a.begin(),it2 = a.end(); it1 != it2; ++it1){
			cout << *it1 << " ";
	}

	system("pause");
	return 0;

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