21C++ STL list 基本操作

一些基本操作

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

int main(){
	 
	list <int> a;//空链表 
	
	list <int> b(10);//10个元素 
	
	list <int> c( 10, 90);// 10 个 90 
	
	vector <int> d( 5, 9);
	list <int> e( d.cbegin(), d.cend());// 5 个 9 
}
#include<list>
#include<iostream>
using namespace std;

template <typename T>
void Display(const T & a){
	for(auto i = a.begin(); i != a.end(); i++)
		cout << *i << ' ';
	cout <<endl;
}

int main(){
	list <int> a;
	
	//末尾插入 
	a.push_back(3);
	a.push_back(4);
	
	//开头插入 
	a.push_front(2);
	a.push_front(1);
	Display( a);
}
#include<list>
#include<iostream>
using namespace std;

template <typename T>
void show(const T& a){
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << *i << ' ';
	cout << endl; 
}

int main(){
	list <int> a;
	
	// 1  a.insert ( 迭代器, const value_type& __x) 
	a.insert( a.begin(), 2);
	a.insert( a.begin(), 1);
	a.insert( a.end(), 3);
	show( a);
	
	// 2  b.insert ( 迭代器, 数量, 值) 
	list <int> b;
	b.insert( b.begin(), 4, 0);
	show( b);
	
	// 3 c.insert ( 指向C中位置的迭代器, 迭代器1, 迭代器2)
	list <int> c;
	c.insert( c.begin(), b.begin(), b.end());
	show( c);
	
}
#include<list>
#include<iostream>
using namespace std;

template <typename T>
void show(const T& a){
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << *i << ' ';
	cout << endl;
}
int main(){
	list <int> a{ 1, 2, 3, 4};
	show( a);
	
	auto p = a.insert( a.begin(), 0); // insert return 指向插入位置的迭代器 
	show ( a);
	
	a.erase( p);// 迭代器 
	show( a);
	
	a.erase( --a.end());// 指向最后一个元素的 迭代器 
	show( a);
	
	a.erase( a.begin(), a.end()); // 迭代器1 ~ 迭代器2 
	cout << a.size() << endl;
}
#include<list>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << *i << ' ';
	cout << endl;
}
int main(){
	list <int> a= { 1, 2, 3, 4, 5};
	show( a); 
	
	a.reverse(); // 反转 
	show( a);
}
#include<list>
#include<iostream>
using namespace std;

template <typename T>
void show( const T& a){
	for(auto i = a.begin(); i != a.end(); i ++)
		cout << *i << ' ';
	cout << endl;
}

//指定新的排序标准 降序 
bool neo(const int& a, const int& c){
	return(a > c);
}

int main(){
	list <int> a{ 1, 3, 2, 5, 4};
	a.sort();//默认升序 排序 
	show( a);
	
	a.sort( neo);// 降序 排序 
	show( a);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值