Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <unordered_map>
- #include <optional>
- typedef std::optional<std::pair<int, int>> returnType;
- // following algorithum works fine: just to show,
- // how I have used the std::optional
- returnType sum_pair(const std::vector<int>& vec, const int sum)
- {
- std::unordered_map<int, int> compIndexMap;
- int index = 0;
- for(const int& ele: vec)
- {
- if(auto check = compIndexMap.find(sum - ele); check != compIndexMap.cend())
- return returnType{std::make_pair(check->second, index)};
- compIndexMap.emplace(sum - ele, index);
- ++index;
- }
- return std::nullopt;
- }
- // problem is here:
- void print_answer(const std::vector<int>& vec, const int sum)
- {
- // if I uncomment the if-else, everything works
- /*if*/(auto Pair = sum_pair(vec, sum) )?
- std::cout << "Resulting indexes are: " << Pair->first << " " << Pair->second << std::endl: //;
- //else
- std::cout << "Nothing found!n";
- }
- int main()
- {
- std::vector<int> vec0{ 1,3,2,8 };
- const int sum = 8;
- print_answer(vec0, sum);
- return 0;
- }
- (condion) ? print something: print something else;
- ||=== Build: Debug in MyTestProgram (compiler: GNU GCC Compiler) ===|
- |25|error: expected primary-expression before 'auto'|
- |25|error: expected ')' before 'auto'|
Add Comment
Please, Sign In to add comment