Guest User

Untitled

a guest
Jul 15th, 2018
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <unordered_map>
  4. #include <optional>
  5.  
  6. typedef std::optional<std::pair<int, int>> returnType;
  7.  
  8. // following algorithum works fine: just to show,
  9. // how I have used the std::optional
  10. returnType sum_pair(const std::vector<int>& vec, const int sum)
  11. {
  12. std::unordered_map<int, int> compIndexMap;
  13.  
  14. int index = 0;
  15. for(const int& ele: vec)
  16. {
  17. if(auto check = compIndexMap.find(sum - ele); check != compIndexMap.cend())
  18. return returnType{std::make_pair(check->second, index)};
  19.  
  20. compIndexMap.emplace(sum - ele, index);
  21. ++index;
  22. }
  23. return std::nullopt;
  24. }
  25.  
  26. // problem is here:
  27. void print_answer(const std::vector<int>& vec, const int sum)
  28. {
  29. // if I uncomment the if-else, everything works
  30. /*if*/(auto Pair = sum_pair(vec, sum) )?
  31. std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl: //;
  32. //else
  33. std::cout << "Nothing found!n";
  34. }
  35. int main()
  36. {
  37. std::vector<int> vec0{ 1,3,2,8 };
  38. const int sum = 8;
  39. print_answer(vec0, sum);
  40. return 0;
  41. }
  42.  
  43. (condion) ? print something: print something else;
  44.  
  45. ||=== Build: Debug in MyTestProgram (compiler: GNU GCC Compiler) ===|
  46. |25|error: expected primary-expression before 'auto'|
  47. |25|error: expected ')' before 'auto'|
Add Comment
Please, Sign In to add comment