STL十大容器 之 list

特点:

内存不连续 ,底层实现是链表 ;
插入和删除的效率比较快 ,随机访问效率比较低;
和vector相比,不再需要 capacity 和 reserve 操作,因为链表没有大小限制 ,不需要为了效率增加预分配内存的功能。

一、插入和删除

push_back() push_front() pop_back() pop_front()
iterator insert( iterator pos, const TYPE &val );
void insert( iterator pos, size_type num, const TYPE &val );
void insert( iterator pos, input_iterator start, input_iterator end );
iterator erase( iterator pos );
iterator erase( iterator start, iterator end );

相较于vector,多了 push_front()和pop_front()两个函数;
erase根据迭代器删除;
remove是根据元素的值来删除;
void remove( const TYPE &val );//删除列表中所有值 等于 val 的元素;
void remove_if( UnPred pr );//按照条件来删除list中的元素,如果pr为true,则删除;
把list中所有的元素val都进行pr(val)调用,如果为 true 则删除;

二、获得容器中的元素

bacK()
front()

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

template<typename IT>
void print(IT beg,IT end){
	for(IT it = beg; it != end; ++it){
		cout << *it << " ";	
	}	
	cout << endl;
}

class Cond{
public:	
	bool operator()(const int& n){
		return n%2!=0;
	}
};

int main(){
	list<int> l;
	l.push_back(1);
	l.push_back(3);
	l.push_back(5);
	l.push_back(7);
	l.push_front(7);
	l.push_front(7);
	l.push_front(2);
	l.push_front(4);
	l.push_front(6);
	l.push_front(8);
	l.push_front(9);
	l.push_front(10);
	l.remove(7);
	print(l.begin(),l.end());
	//删除所有奇数
	l.remove_if(Cond());
	print(l.begin(),l.end());
	return 0;	
}

程序举例:

在list里存储学生信息,删除成绩不及格的学生;

#include <iostream>
#include <list>
using namespace std;
template<typename IT>
void print(IT beg,IT end){
	for(IT it = beg; it != end; ++it){
		cout << *it << endl;	
	}	
	cout <<"___________"<< endl;
}

class Stu{
public:
	int no;
	string name;
	int age;
	int score;
	Stu(int no=0,string name="",int age=0,int score=0):no(no),name(name),age(age),score(score){}
	bool operator==(const Stu& s){
		return no==s.no&&name==s.name&&s.age==age&&score==s.score;	
	}
	friend ostream& operator<<(ostream& os,const Stu& s){
		return os<<s.no<<":"<<s.name<<":"<<s.age<<":"<<s.score;	
	}
};

class Cond{
public:
	bool operator()(const Stu& s){
		return s.score < 60;	
	}
};

int main(){
	list<Stu> l;
	l.push_back(Stu(110,"zhang",13,90));
	l.push_back(Stu(120,"zhang",13,89));
	l.push_back(Stu(130,"zhang",13,56));
	l.push_back(Stu(140,"zhang",13,30));
	l.push_back(Stu(150,"zhang",13,99));
	l.push_back(Stu(160,"zhang",13,19));
	l.push_back(Stu(170,"zhang",13,55));
	l.push_back(Stu(180,"zhang",13,90));

	l.remove_if(Cond());
	print(l.begin(),l.end());
	return 0;	
}

三、迭代器

begin() end() rbegin() rend()

四、 size() resize() clear()

参考vector;

五、swap()

参考vector;

六、merge 合并

void merge( list &lst );
void merge( list &lst, Comp compfunction );
如果列表都有序 则合并之后可以保持有序

七、reverse 逆序

参考vector;

八、splice

合并 把另外一list指定区域的元素插入到当前list

void splice( iterator pos, list &lst );
void splice( iterator pos, list &lst, iterator del );
void splice( iterator pos, list &lst, iterator start, iterator end );

九 、sort

排序 全局的函数不支持list的排序 list有一个成员函数sort用于排序
void sort();
void sort( Comp compfunction );

十、unique

删除重复元素 删除列表中相临的重复的元素

void unique();
void unique( BinPred pr );

参考代码:

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

template<typename IT>
void print(IT beg,IT end){
	for(IT it = beg; it != end; ++it){
		cout << *it << " ";	
	}	
	cout << endl;
}

int main(){
	list<int> l1;
	l1.push_back(6);
	l1.push_back(8);
	l1.push_back(10);
	l1.push_back(5);
	l1.push_back(2);
	
	list<int> l2;
	l2.push_back(11);
	l2.push_back(2);
	l2.push_back(7);
	l2.push_back(15);
	
	l1.merge(l2);
	print(l1.begin(),l1.end());
	
	list<int> l3;
	l3.push_back(2);
	l3.push_back(6);
	l3.push_back(10);
	list<int> l4;
	l4.push_back(4);
	l4.push_back(5);
	l4.push_back(7);
	l4.push_back(9);
	l4.push_back(11);
	l3.merge(l4);
	print(l3.begin(),l3.end());
	l3.reverse();
	print(l3.begin(),l3.end());

	list<int> l5;
	l5.push_back(1);
	l5.push_back(1);
	l5.push_back(3);
	l5.push_back(3);
	l5.push_back(1);
	l5.push_back(3);
	l5.push_back(3);
	l5.push_back(1);
	l5.unique();
	print(l5.begin(),l5.end());
	return 0;	
}
发布了53 篇原创文章 · 获赞 18 · 访问量 7241

猜你喜欢

转载自blog.csdn.net/Nire_Yeyu/article/details/100938315