类排序

#include<iostream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
class item
{
private:
	std::string  m_itemid;
	int m_price;
	int m_count;
public:
	item(std::string id, int price, int count) :
		m_itemid(id), m_count(count), m_price(price) {}
	int getcount() const {
		return m_count;
	}
	std::string getitemid() const {
		return m_itemid;
	}
	int getprice() const {
		return m_price;
	}
	bool operator==(const item & obj2) const
	{
		if (this->getitemid().compare(obj2.getitemid()) == 0)
			return true;
		else
			return false;
	}

	bool compare(int num)
	{
		if (this->getprice() == num)
			return true;
		else
			return false;
	}

	static bool sort(item & obj1, item & obj2)
	{
		return obj1.getcount() < obj2.getcount();
	}

	bool operator<(const item & obj) const
	{
		return getcount() < obj.getcount();
	}
};

bool pricecomparision(item & obj, int y)
{
	if (obj.getprice() == y)
		return true;
	else
		return false;
}

bool SortFun(item & obj1, item & obj2)
{
	return obj1.getcount() < obj2.getcount();
}

std::vector<item> getitemlist()
{
	std::vector<item> vecofitems;
	vecofitems.push_back(item("d121", 100, 2));
	vecofitems.push_back(item("d122", 12, 5));
	vecofitems.push_back(item("d123", 28, 6));
	vecofitems.push_back(item("d124", 8, 10));
	vecofitems.push_back(item("d125", 99, 3));
	return vecofitems;
}

int main()
{
	std::vector<item> vecofitems = getitemlist();
	std::vector<item>::iterator it;
	std::vector<int> vec = { 1, 2, 3 };
	std::vector<int>::iterator it1 = vec.begin()+2;
	
	std::cout << *it1 << endl;
	//it = std::find_if(vecofitems.begin(), vecofitems.end(), std::bind(pricecomparision, std::placeholders::_1, 28));
	int b = 28;
	it = std::find_if(vecofitems.begin(), vecofitems.end(), std::bind(&item::compare, std::placeholders::_1, b));
	if (it != vecofitems.end())
		std::cout << "item price ::" << it->getprice() << " count :: " << it->getcount() << std::endl;
	else
		std::cout << "item not found" << std::endl;
	std::cout << "sort "<< std::endl;
	//std::sort(vecofitems.begin(), vecofitems.end(), &item::sort);
	//std::sort(vecofitems.begin(), vecofitems.end());
	std::sort(vecofitems.begin(), vecofitems.end(), [](item & obj1, item & obj2){return obj1.getcount() < obj2.getcount(); });
	for (std::vector<item>::iterator iter = vecofitems.begin(); iter != vecofitems.end(); iter++)
	{
		std::cout << iter->getcount() << " ";
	}
	std::cout << std::endl;
	system("pause");
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值
>