fork download
  1. #include <map>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <stdexcept>
  5.  
  6. //functor
  7. struct finder {
  8. finder(int value_to_find) : v(value_to_find){}
  9. bool operator()(const std::pair<const int, int>& node)
  10. {return node.second == v;}
  11. protected:
  12. int v;
  13. };
  14.  
  15. int find_value(const std::map<int, int>& container, int value_to_find) {
  16. typename std::map<int, int>::const_iterator it;
  17. it = std::find_if(container.begin(), container.end(), finder(value_to_find));
  18. if (it == container.end())
  19. throw std::runtime_error("value is not in container!");
  20. return it->first;
  21. }
  22.  
  23. int main() {
  24. std::map<int, int> container;
  25. container[3] = 7;
  26. std::cout << find_value(container, 7);
  27. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp: In function ‘int find_value(const std::map<int, int, std::less<int>, std::allocator<std::pair<const int, int> > >&, int)’:
prog.cpp:16: error: using ‘typename’ outside of template
stdout
Standard output is empty