#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
class Stock
{
public:
Stock(long n, double a, double b);
Stock() {};
void Show();
long GetShares();
long shares;
double share_val;
double total_val;
bool operator == (const int& i) const
{
return this->shares == i;
}
bool cmp()
{
return this->shares == 4;
}
};
class findx {
public:
findx(const int i){ num = i; }
bool operator()(Stock& dValue) {
if (dValue.shares == num)
return true;
else
return false;
}
private:
int num;
};
Stock::Stock(long n, double a, double b)
{
shares = n;
share_val = a;
total_val = b;
}
long Stock::GetShares()
{
return shares;
}
void Stock::Show()
{
cout << "shares = " << shares << endl;
cout << "share_val = " << share_val << endl;
cout << "total_val = " << total_val << endl;
}
bool cmp(Stock stock)
{
return stock.shares == 4;
}
bool cmp2(int a)
{
return a == 5;
}
bool CompareShares(Stock &stock1, int num)
{
return stock1.shares == num;
}
struct compare : binary_function<Stock, int, bool> {
bool operator()(Stock &value, int num) const
{
if (value.shares == num)
return true;
else
return false;
}
};
int main()
{
Stock stock1(1, 2, 3);
//stock1.Show();
Stock stock2(4, 5, 6);
Stock stock3(7, 8, 9);
vector<Stock> s(10);
s.push_back(stock1);
s.push_back(stock2);
s.push_back(stock3);
int size = s.size();
for (int i = 0; i < size; i++)
{
if (s[i].shares == 4)
{
s[i].shares = 5;
cout << "find it" << endl;
}
}
//cout << stock2.shares << endl;
s[1].shares = 5;
cout << stock2.shares << endl;
vector<Stock>::iterator iter = find_if(s.begin(), s.end(), findx(4));
//vector<Stock>::iterator iter = find_if(s.begin(), s.end(), cmp);
//vector<Stock>::iterator iter = find_if(s.begin(), s.end(), bind2nd(compare(), 4));
//vector<Stock>::iterator iter = find_if(s.begin(), s.end(), [](Stock &value) {return value == 4; });
//vector<Stock>::iterator iter = find_if(s.begin(), s.end(), bind2nd(ptr_fun(CompareShares), 4));
if (iter != s.end())
{
iter->shares = 5;
cout << iter->shares << endl;
}
else
cout << "not find it" << endl;
cout << stock2.shares << endl;
cout << s[1].shares << endl;
system("pause");
return 0;
}
#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;
}
};
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));
it = std::find_if(vecOfItems.begin(), vecOfItems.end(), std::bind(&Item::Compare, 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;
}