现在的位置: 首页 > 综合 > 正文

关于c++ list容器的操作摸索

2019年11月09日 ⁄ 综合 ⁄ 共 1213字 ⁄ 字号 评论关闭
#include<iostream>
#include<algorithm>
#include<list>
#include<iterator>
#include<cstdlib>
using namespace std;
struct n1
{
	string name;
	long long num;
};
list<n1>l;
void charu()
{
	n1 tmp;
	cin>>tmp.name>>tmp.num;
	if(tmp.num<l.front().num)
		l.push_front(tmp);
	else
		l.push_back(tmp);
}
void quanshan()
{
	l.clear();
}
string xs;
long long xi;
bool way(const n1 & a)
{
	if(xi==-1)
		return a.name==xs;
	else
		return a.num==xi;
}
void shanmingzi()
{
	xi=-1;
	cin>>xs;
	l.remove_if(way);
}
void shanxuehao()
{
	cin>>xi;
	l.remove_if(way);
}
void xianshi()
{
	list<n1>::iterator i;
	for(i=l.begin();i!=l.end();i++)
		cout<<i->name<<","<<i->num<<endl;
}
bool cmp(n1 a,n1 b)
{
	return a.num<b.num;
}
void shunxu()
{
	l.sort(cmp);
}
void daoxu()
{
	l.reverse();
}


bool same_integral_part (n1 a,n1 b)
{
	 return (a.name==b.name)&&(a.num==b.num); 
}

void xuanze()
{
	int j;
	cout<<"1.插入 2.删除 3.查看 4.排序"<<endl;
	cin>>j;
	if(j==1)
	{
		cout<<"输入名字,学号(例如,杜甫 123):"<<endl;
		charu(); 
	}
	else if(j==2)
	{
		cout<<"1.按名字删 2.按学号删 3.全删"<<endl;
		cin>>j;
		if(j==1)
			shanmingzi();
		else if(j==2)
			shanxuehao();
		else if(j==3)
			quanshan();
	}
	else if(j==3)
	{
		xianshi();
	}
	else if(j==4)
	{
		cout<<"1.顺序 2.倒序"<<endl;
		cin>>j;
		if(j==1)
			shunxu();
		else if(j==2)
			daoxu();
		cout<<"1.去重"<<endl;
		cin>>j;
		if(j==1)
			l.unique(same_integral_part); 
	}
}
int main()
{
	int j;
	while(1)
	{
		xuanze();
		cout<<"0.退出 1.返回选择界面"<<endl; 
		cin>>j;
		if(j==0)
			return 0;
		system("cls");
	}
}

【上篇】
【下篇】

抱歉!评论已关闭.