C++容器vector,list,map用法总结

vector容器就像数组一样,但是比数组使用要方便的多

遍历,排序,删除,查找操作汇总如下。


class test
{
    private:
        int n,i;
        vector<int>a;
        vector<int>::iterator p;
    public:
    test(int nn)
    {
        n=nn;
        srand(time(0));
        for(i=0;i<n;++i)
        {
            a.push_back(rand());
        }
    }
    void browse()
    {
        for(p=a.begin();p!=a.end();++p)
        {
            cout<<*p<<endl;
        }    
    }
    static bool sort1(int n1,int n2)
    {
        return n1>n2;
    }
    void Sort()
    {
        sort(a.begin(),a.end(),sort1);
    }    
    void Erase(int m)
    {
        p=a.begin()+m;//三个容器只有vector可以使用偏移量
        a.erase(p);
    }
    void Find()
    {
        int temp;
        cout<<"input:";cin>>temp;
        p=a.begin();
        while(1)
        {
            p=find(p,a.end(),temp);
            if(p!=a.end())
            {
                cout<<*p<<endl;
                ++p;
            }
            else
            break;
        }
    }
};

这是vector容器中元素类型为结构体的遍历,删除,查找,排序操作。

struct S
{
    int number;int score;int index;
    bool operator==(S s)//对find()查找函数定义,这个运算符重载要放在结构体中,find函数会按照这个方式查找,注意参数
    {
        if(s.index==1)
        return score==s.score;
        else
        return number==s.number;
    }
};
class test{
    private:
        int i,n;
        vector<S>a;
        vector<S>::iterator p;
    public:
        test(int nn)
        {
            S temp;
            srand(time(0));
            n=nn;
            for(i=0;i<n;++i)
            {
                temp.number=rand();
                temp.score=rand();
                a.push_back(temp);    
            }
        }
        void browse()
        {
            cout<<"================="<<endl;
            for(p=a.begin();p!=a.end();++p)
            {
                cout<<p->number<<"-"<<p->score<<endl;
            }
        }
        static bool sort1(S n1,S n2)
        {
            return n1.number>n2.number;
        }
        void Sort()
        {
            sort(a.begin(),a.end(),sort1);
        }
        void Erase(int m)
        {
            p=a.begin()+m;
            a.erase(p);
        }
        void Find()
        {
            S temp;temp.index=1;
            cout<<"input the number you want to check:";cin>>temp.score;
            p=a.begin();
            while(1)
            {
                p=find(p,a.end(),temp);
                if(p!=a.end())
                {
                    cout<<p->number<<"-"<<p->score<<endl;
                    ++p;//要加上++P否则p将永远不等于a.end() 
                }
                else
                break;
            }
        }
};

这是list容器的基本常用操作,list容器插入删除元素 效率十分高。

class test
{
	private:
		int n,i;
		list<int>a;
		list<int>::iterator p;
	public:
	test(int nn)//主函数规定数目
	{
		n=nn; 
		srand(time(0));
		for(i=0;i<n;++i)
		{
			a.push_back(rand());
		}
	}
	void browse()//遍历
	{
		cout<<"======================"<<endl;
		for(p=a.begin();p!=a.end();++p)
		{
			cout<<*p<<endl;
		}	
	}
	static bool sort1(int n1,int n2)//排序规则函数
	{
		return n1>n2;
	}
	void Sort()//排序
	{
		a.sort(sort1);//比vector效率低一半 
	}	
	void Erase(int m)
	{
		int i=0;
		p=a.begin();
		for(i=0;i<m;++i)
		{
			++p;
		}
		a.erase(p); 
	}
	void Find1()//输入要查找的数字并输出
	{
		int temp;
		cout<<"input:";cin>>temp;
		p=a.begin();
		while(1)
		{
			p=find(p,a.end(),temp);
			if(p!=a.end())
			{
			    cout<<*p<<endl;
				++p;//注意要将指针挪动
			}
			else
			{
				break;
			}
		}
	}
	void Find2()//查找元素并删除
	{
		int temp;
		cout<<"input:";cin>>temp;
		p=a.begin();
		while(1)
		{
			p=find(p,a.end(),temp);
			if(p!=a.end())
			{
				p=a.erase(p);//a.erase(p++)也可以,但是a.erase()不可以   
			}
			else
			break;
		}
	}
	void Insert()
	{
		p=a.begin(); 
		a.insert(p,12345);//在p的前面插入一个元素 
	}
};

 find_if()函数

static bool find1(S s)//自己修改条件 
{
	return s.number==s.score;
}
void Find1()
{
	S temp;
//	cout<<"input number:"<<endl;cin>>temp.number;
	p=a.begin();
	while(1)
	{
		p=find_if(p,a.end(),find1);//find_if函数前面要定义查找规则类似于sort查找规则 
		if(p!=a.end())
		{
			cout<<p->number<<":"<<p->score<<endl;
			++p;
		}
		else
		break;
	}
}

 

这是map容器的基本操作,因为map容器检索数据效率十分高,所以只列出检索函数 

class test{
	private:
		int n,i;
		map<int,int>a;//键值,第一项必须是一个不重复的数,这是map容器不同于其他容器的一点
		map<int,int>::iterator p;
	public:
		test(int nn)
		{
			n=nn;
			srand(time(0));
			for(i=0;i<n;++i)
			{
				a.insert(pair<int,int>(rand(),rand()));//a.insert()的参数是一个数对
			}
		}
		void browse()
		{
			for(p=a.begin();p!=a.end();++p)
			{
				cout<<p->first<<"-"<<p->second<<endl;//->first指第一个键值,->second指第二个
			}
		}
		void Find()
		{
			int temp;
			cout<<"input key:";cin>>temp;
			p=a.find(temp);//a.find()只有一个参数,就是要寻找的键值,并且因为键值没有重复                            
                            //的,并不需要while(1)循环
			if(p!=a.end())
			{
	     		cout<<p->first<<"-"<<p->second<<endl;
			}
			else 
			cout<<"no find"<<endl;
		}
};//数据固定没有变化,在整体规模不变下查询具体数据,用map,效率非常高 

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值