#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;
}