fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. class class1
  9. {
  10. private:
  11. int id;
  12. double value;
  13.  
  14. public:
  15. class1(int i, double v):id(i), value(v){ }
  16. int getId()const {return id;}
  17. double getValue() const {return value;}
  18. };
  19.  
  20. class HasIdentifier:public unary_function<class1, bool>
  21. {
  22. public:
  23. HasIdentifier(int id) : m_id(id) { }
  24. bool operator()(const class1& c)const
  25. {
  26. return (c.getId() == m_id);
  27. }
  28. private:
  29. int m_id;
  30. };
  31.  
  32.  
  33. class class2
  34. {
  35. private:
  36. vector <class1> objects;
  37. public:
  38. class2()
  39. {
  40. objects.push_back(class1(1, 100.0));
  41. objects.push_back(class1(2, 100.0));
  42. objects.push_back(class1(3, 100.0));
  43. }
  44.  
  45. double GetValueOfId(int id)
  46. {
  47. vector<class1>::iterator itElem = find(objects.begin(), objects.end(), HasIdentifier(id));
  48. return itElem->getValue();
  49. }
  50. };
  51.  
  52. int main() {
  53.  
  54. class2 c;
  55.  
  56. int id = 2;
  57.  
  58. cout<< id << " " << c.GetValueOfId(id);
  59. return 0;
  60. }
  61.  
  62.  
  63.  
  64. // Then, to find it:
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
/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’
stdout
Standard output is empty