Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 递归函数没有特定停止规则时的时间复杂度_C++_Time Complexity - Fatal编程技术网

C++ 递归函数没有特定停止规则时的时间复杂度

C++ 递归函数没有特定停止规则时的时间复杂度,c++,time-complexity,C++,Time Complexity,这是一个递归函数。它遍历字符串映射(multimap-graph)。检查itr->second(s_tmp),如果s_tmp等于所需字符串(Exp),则打印它(itr->first),并再次为该itr->first执行该函数 string findOriginalExp(string Exp){ cout<<"*****findOriginalExp Function*****"<<endl; for(auto itr=graph.be

这是一个递归函数。它遍历字符串映射(
multimap-graph
)。检查
itr->second
(s_tmp),如果
s_tmp
等于所需字符串(
Exp
),则打印它(
itr->first
),并再次为该
itr->first
执行该函数

string findOriginalExp(string Exp){
    cout<<"*****findOriginalExp Function*****"<<endl;
    for(auto itr=graph.begin();itr!=graph.end();itr++){
        string s_tmp = itr->second;
        string f_tmp = itr->first;
        string nll = "null";

        if(s_tmp.compare(Exp) == 0){
            if(f_tmp.compare(nll) == 0){
                cout<< Exp <<" :is original experience.";
                return Exp;
             }else{
                return findOriginalExp(itr->first);
             }
        } 
    }
}
string findOriginalExp(string Exp){

问题是:您的函数不正确。除非您对
图形的内容有非常特殊的前提条件(您没有告诉我们),否则您的代码将调用未定义的beahvior

也许编译器比我更能说服你。我在你的代码中添加了缺失的部分,以获得以下结果:

#include <string>
#include <map>
#include <iostream>

using namespace std;

std::map<std::string,std::string> graph;

string findOriginalExp(string Exp){
    cout<<"*****findOriginalExp Function*****"<<endl;
    for(auto itr=graph.begin();itr!=graph.end();itr++){
        string s_tmp = itr->second;
        string f_tmp = itr->first;
        string nll = "null";

        if(s_tmp.compare(Exp) == 0){
            if(f_tmp.compare(nll) == 0){
                cout<< Exp <<" :is original experience.";
                return Exp;
             }else{
                return findOriginalExp(itr->first);
             }
        } 
    }
}

int main(int argc, char *argv[]){
}
不从声明为返回内容的函数返回内容是未定义的行为。在存在未定义行为的情况下,您的代码可能看起来正常工作,但仍处于中断状态


讨论该函数的时间复杂度是没有意义的(除非你包含了一些先决条件,以确保在考虑因素时不会遇到任何未定义的行为)。

问题是:你的函数是不正确的。除非你对
图形的内容有非常特殊的先决条件(你没有告诉我们)您的代码调用未定义的beahvior

也许编译器比我更能说服你。我在你的代码中添加了缺失的部分,以获得以下结果:

#include <string>
#include <map>
#include <iostream>

using namespace std;

std::map<std::string,std::string> graph;

string findOriginalExp(string Exp){
    cout<<"*****findOriginalExp Function*****"<<endl;
    for(auto itr=graph.begin();itr!=graph.end();itr++){
        string s_tmp = itr->second;
        string f_tmp = itr->first;
        string nll = "null";

        if(s_tmp.compare(Exp) == 0){
            if(f_tmp.compare(nll) == 0){
                cout<< Exp <<" :is original experience.";
                return Exp;
             }else{
                return findOriginalExp(itr->first);
             }
        } 
    }
}

int main(int argc, char *argv[]){
}
不从声明为返回内容的函数返回内容是未定义的行为。在存在未定义行为的情况下,您的代码可能看起来正常工作,但仍处于中断状态


讨论该函数的时间复杂度是没有意义的(除非您包含了一些先决条件,以确保在考虑因素时不会遇到任何未定义的行为)。

您的代码相当于以下代码,我认为这些代码更具可读性:

std::string findOriginalExp(std::string Exp)
{
    std::cout << "*****findOriginalExp Function*****" << std::endl;
    auto it = std::find_if(graph.begin(), graph.end(), [&](auto& p){ return p.second == Exp; });
    // assert(it != graph.end());
    if (it->first == "null") {
        std::cout << Exp << " :is original experience.";
        return Exp;
    }
    return findOriginalExp(it->first);
}
std::string findOriginalExp(std::string Exp)
{

std::cout您的代码相当于下面的代码,我觉得更可读:

std::string findOriginalExp(std::string Exp)
{
    std::cout << "*****findOriginalExp Function*****" << std::endl;
    auto it = std::find_if(graph.begin(), graph.end(), [&](auto& p){ return p.second == Exp; });
    // assert(it != graph.end());
    if (it->first == "null") {
        std::cout << Exp << " :is original experience.";
        return Exp;
    }
    return findOriginalExp(it->first);
}
std::string findOriginalExp(std::string Exp)
{

std::如果我在执行函数之前为映射设置一个if条件来检查它是否为空,这个问题可以解决吗?否,因为可能没有
s\u tmp.compare(Exp)==0
为真的元素(顺便说一句,为什么不干脆
s\u tmp==Exp
)@simsim我强烈建议您使用调试器查看代码中实际发生的情况(编译器不太可能在尝试利用UB时出错,因为它需要知道参数才能使用它)@simsim尝试一个示例,其中
s_tmp.compare(Exp)==0
永远都不是
真的
@simsim预测下一步可能发生的事情:请不要修改此问题以要求完全其他的东西(修复代码将使问题变成其他东西)。相反,我建议您打开一个新问题,该问题可以处理希望正确的代码的复杂性。如果我在执行函数之前为映射设置一个if条件来检查它是否为空,这个问题会得到解决吗?否,因为可能没有
s\u tmp.compare(Exp)==0
为真的元素(顺便说一句,为什么不仅仅是
s_tmp==Exp
)@simsim我强烈建议您使用调试器来查看代码中实际发生了什么(编译器不太可能在尝试利用UB时出错,因为它需要知道参数才能使用它)@simsimsim尝试一个
s_tmp.compare(Exp)的示例==0
永远都不是
真的
@simsim预测下一步可能发生的事情:请不要修改此问题以要求完全其他的东西(修复代码将使问题变成其他东西)。相反,我建议您打开一个新问题,它可以处理希望正确的代码的复杂性。否,此代码在程序中运行不正常。它卡在一个循环中。什么是
图形
?它是否包含一对
std::string
const char*
?后者确实存在重写问题(
it->first==“null”
随时都会导致错误)。图形是多重映射的。(
multimap graph
)。不,此代码在程序中运行不正常。它卡在循环中。什么是
graph
?它是否包含一对
std::string
const char*
?后者确实存在重写问题(
it->first==“null”
随时都会导致false)。图形是多重映射的。(
multimap graph
)。注释不用于扩展讨论;此对话已被删除。注释不用于扩展讨论;此对话已被删除。