zoukankan      html  css  js  c++  java
  • 关于c++ list容器的操作摸索

    版权声明:本文为博主原创文章,未经博主同意不得转载。 https://blog.csdn.net/chaoweilanmao/article/details/30793859
    #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");
    	}
    }

  • 相关阅读:
    Python全栈_Day3_网络基础
    Python全栈_Day2_操作系统基础
    Python全栈_Day1_计算机硬件
    NX二次开发-以指定字符分割字符串
    NX二次开发-获取当前项目路径
    NX二次开发-根据文件名删除文件
    NX二次开发-写入信息窗口
    NX二次开发-读文件
    NX二次开发-获取NX里的对象信息
    NX二次开发-菜单
  • 原文地址:https://www.cnblogs.com/mqxnongmin/p/10685410.html
Copyright © 2011-2022 走看看