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;

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