1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| #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 priceComparision(Item & obj, int y) { if (obj.getPrice() == y) return true; else return false; }
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; it = std::find_if(vecOfItems.begin(), vecOfItems.end(), std::bind(priceComparision, std::placeholders::_1, 28)); if (it != vecOfItems.end()) std::cout << "Item Price ::" << it->getPrice() << " Count :: " << it->getCount() << std::endl; else std::cout << "Item not Found" << std::endl; return 0; }
|