我正在尝试 C++17 功能std::optional
可选的返回类型是std::optional<std::pair<int, int>>. 我在函数中调用该
sum_pair函数print_answer并想要一个可选的打印。
在print_answer函数中,我想检查所需的对是否包含要显示的内容。就像下面给出的示例一样:可选返回工厂函数可用作 while 和 if 的条件
以下是代码:这里有错误
#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 …Run Code Online (Sandbox Code Playgroud)