如何让一个函数有不同的返回类型?C++ [英] How to have a function have different return type? C++

查看:40
本文介绍了如何让一个函数有不同的返回类型?C++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一项作业,我的教授将这项任务作为作业的一部分交给了我们.我对 C++ 还是比较陌生的,所以任何帮助.我创建了该函数,但是我不明白他如何在未找到任何内容时返回一个空白字符串,但如果找到则返回一个整数.在没有重载功能的情况下,这可能吗?现在我只是将函数设置为整​​数类型,而不是返回一个空白字符串,而是返回 0.

I am doing an assignment and my prof gave us this task as part of the assignment. I am still relatively new to C++ so any help. I created the function however I don't understand how he wants to return a blank string if nothing is found but return an integer if it is found. Is that possible somehow without overloading functions? Right now I just have the function set to an integer type and instead of returning a blank string it returns 0 instead.

这是作业的一部分.如果您能解释您的解决方案,以便像我这样愚蠢的人能够理解它,那将不胜感激.

Here is the task that is part of the assignment. If you could explain your solution so that a dumb person like me can understand it that would be much appreciated.

FindWord——这个成员函数将一个字符串作为参数并在列表.如果列表为空或找不到,则返回一个空字符串值(").否则,它返回索引的 int 值(例如数组元素编号,例如 0、1、2 等).请记住 == 相等比较运算符适用于作为字符串处理的字符串对象(而不是 char 数据元素)并且您不需要区分大小写.

FindWord – this member function will take a string as a parameter and search for it within the list. If the list is empty or it cannot be found, it returns a blank string value (""). Otherwise, it returns the int value of the index (e.g. the array element number, such as 0, 1, 2, etc.). Remember that the == equality comparison operator works on strings that are handled as string objects (rather than char data elements) and that you do not need to be case sensitive.

这是我到目前为止的代码

Here is my code I have so far

int WordList::FindWord(string word){

if ((listSize = 0)){ // return -1 if no list exists;
    return -1;
}
else {
    for (int i =0;i<listSize;i++){ // run loop to check for word/        
        if (word == list[i]) // if word is found then return index
            return i;
     }        
        return -1; // return -1 if word is not found
   }
}

推荐答案

这可以通过 C++17 特性 std::variantstd::get_if 来完成.

This can be done with C++17 features std::variant and std::get_if.

  • 搜索函数将返回std::variant
  • main 中使用 std::get_if 来确定哪些值已返回.
  • The search function will return std::variant<int, std::string>
  • In main use std::get_if to determine which of the values has been returned.

std::get_if 返回指向存储在指向变量中的值的指针或错误时的空指针.

std::get_if returns pointer to the value stored in the pointed-to variant or null pointer on error.

#include <variant>
#include <string>
#include <iostream>
#include <list>

 std::variant<int, std::string> strSearch(const std::string& searchStr, const std::list<std::string>& searchList)
 {
     int i = 0;
     for(const auto& s: searchList) {               
        if(!s.find(searchStr))
            return i;
        i++;    
     }
     return "";     
 }

int main()
{
    std::list<std::string> strList = {"stack", "stackover", "stackoverflow"};
    std::string str = "stackoverflow";
    std::variant<int, std::string> v{strSearch(str, strList)};
    if(std::get_if<int>(&v))
        std::cout << "String found. Index in the List is:" << std::get<int>(v) << '\n';
    else    
        std::cout << "String not found"  << '\n';

    str = "stackunderflow";
    v = strSearch(str, strList);
    if(std::get_if<int>(&v))
        std::cout << "String found. Index in the List is:" << std::get<int>(v)  << '\n';
    else    
        std::cout << "String not found."  << '\n';    

}

此处现场观看.

这篇关于如何让一个函数有不同的返回类型?C++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆