fork(3) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. using namespace std;
  5.  
  6. struct p {
  7. int x,y;
  8. p(const int x, const int y) : x(x), y(y) {};
  9. bool operator==(const p& other) const { return x == other.y && y == other.y; };
  10. };
  11.  
  12. int findInVector(const std::vector<p> & vecOfElements, const p & element)
  13. {
  14. int result;
  15.  
  16. auto it = std::find_if(vecOfElements.begin(), vecOfElements.end(), [&temp = element]
  17. (const p& pp) -> bool { return temp == pp; });
  18.  
  19. if (it != vecOfElements.end())
  20. {
  21. result = distance(vecOfElements.begin(), it);
  22. }
  23. else
  24. {
  25. result = -1;
  26. }
  27.  
  28. return result;
  29. }
  30.  
  31. int main(){
  32. p test = p(1,2);
  33. std::vector<p> pvector;
  34. pvector.push_back(test);
  35. std::cout << findInVector(pvector,test);
  36. }
Success #stdin #stdout 0s 4368KB
stdin
Standard input is empty
stdout
-1