#include <iostream>
#include <vector>
#include <functional>
#include <algorithm>
using namespace std;
class class1
{
private:
int id;
double value;
public:
class1(int i, double v):id(i), value(v){ }
int getId()const {return id;}
double getValue() const {return value;}
};
class HasIdentifier:public unary_function<class1, bool>
{
public:
HasIdentifier(int id) : m_id(id) { }
bool operator()(const class1& c)const
{
return (c.getId() == m_id);
}
private:
int m_id;
};
class class2
{
private:
vector <class1> objects;
public:
class2()
{
objects.push_back(class1(1, 100.0));
objects.push_back(class1(2, 100.0));
objects.push_back(class1(3, 100.0));
}
double GetValueOfId(int id)
{
vector<class1>::iterator itElem = find(objects.begin(), objects.end(), HasIdentifier(id));
return itElem->getValue();
}
};
int main() {
class2 c;
int id = 2;
cout<< id << " " << c.GetValueOfId(id);
return 0;
}
// Then, to find it:
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8ZnVuY3Rpb25hbD4KI2luY2x1ZGUgPGFsZ29yaXRobT4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBjbGFzczEKewpwcml2YXRlOgogIGludCBpZDsKICBkb3VibGUgdmFsdWU7CgpwdWJsaWM6CmNsYXNzMShpbnQgaSwgZG91YmxlIHYpOmlkKGkpLCB2YWx1ZSh2KXsgfQogIGludCBnZXRJZCgpY29uc3Qge3JldHVybiBpZDt9CiAgZG91YmxlIGdldFZhbHVlKCkgY29uc3Qge3JldHVybiB2YWx1ZTt9Cn07CgpjbGFzcyBIYXNJZGVudGlmaWVyOnB1YmxpYyB1bmFyeV9mdW5jdGlvbjxjbGFzczEsIGJvb2w+ICAKeyAgCnB1YmxpYzoKICAgIEhhc0lkZW50aWZpZXIoaW50IGlkKSA6IG1faWQoaWQpIHsgfSAgCiAgICBib29sIG9wZXJhdG9yKCkoY29uc3QgY2xhc3MxJiBjKWNvbnN0ICAKICAgIHsgIAogICAgICAgIHJldHVybiAoYy5nZXRJZCgpID09IG1faWQpOyAgCiAgICB9ICAKcHJpdmF0ZToKICAgIGludCBtX2lkOyAgCn07ICAKCgpjbGFzcyBjbGFzczIKewpwcml2YXRlOgogICAgdmVjdG9yIDxjbGFzczE+IG9iamVjdHM7CnB1YmxpYzoKICBjbGFzczIoKQogIHsKICAgIG9iamVjdHMucHVzaF9iYWNrKGNsYXNzMSgxLCAxMDAuMCkpOwogICAgb2JqZWN0cy5wdXNoX2JhY2soY2xhc3MxKDIsIDEwMC4wKSk7CiAgICBvYmplY3RzLnB1c2hfYmFjayhjbGFzczEoMywgMTAwLjApKTsKICB9CgogIGRvdWJsZSBHZXRWYWx1ZU9mSWQoaW50IGlkKQogIHsKICAgIHZlY3RvcjxjbGFzczE+OjppdGVyYXRvciBpdEVsZW0gPSBmaW5kKG9iamVjdHMuYmVnaW4oKSwgb2JqZWN0cy5lbmQoKSwgSGFzSWRlbnRpZmllcihpZCkpOwkKICAgIHJldHVybiBpdEVsZW0tPmdldFZhbHVlKCk7CiAgfQp9OwoKaW50IG1haW4oKSB7CgkKCWNsYXNzMiBjOwoKCWludCBpZCA9IDI7CgkKCWNvdXQ8PCBpZCA8PCAiICIgPDwgYy5HZXRWYWx1ZU9mSWQoaWQpOwoJcmV0dXJuIDA7Cn0KCgoKLy8gVGhlbiwgdG8gZmluZCBpdDo=
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h: In function ‘_RandomAccessIterator std::__find(_RandomAccessIterator, _RandomAccessIterator, const _Tp&, std::random_access_iterator_tag) [with _RandomAccessIterator = __gnu_cxx::__normal_iterator<class1*, std::vector<class1, std::allocator<class1> > >, _Tp = HasIdentifier]’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:3814: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<class1*, std::vector<class1, std::allocator<class1> > >, _Tp = HasIdentifier]’
prog.cpp:47: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:178: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:3814: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = __gnu_cxx::__normal_iterator<class1*, std::vector<class1, std::allocator<class1> > >, _Tp = HasIdentifier]’
prog.cpp:47: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:182: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:186: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:190: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:198: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:202: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_algo.h:206: error: no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = class1*, _Container = std::vector<class1, std::allocator<class1> >]() == __val’