递归函数构建一个向量,会返回什么



所以我试图递归地构建一个向量,当我看到这个时,我开始认为我做得很错误。下面的代码会在每次迭代结果中返回一个向量吗?还是我只是在每次迭代中创建新的向量,而实际上不会在每次递归调用中构建。如果我错了,我该如何递归地构建向量。。。提前感谢您的建设性帮助!

std::vector<ParameterClass> recursiveParser :: parseParamList()
{
  std::vector<ParameterClass> paramVector;
  if (lexicator->getCurrentToken()->getTokenType() == STRING) {
    paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
    lexicator->advance();
    parseParamList();
  } else if (lexicator->getCurrentToken()->getTokenType() == ID) {
    paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
    lexicator->advance();
    parseParamList();
  } else {
  // so as to not fail in Expression, i need to check to see that there is a
  // left paren indicating that there should be an expression
    if (lexicator->getCurrentToken()->getTokenType() == LEFT_PAREN) {
      paramVector.push_back(ParameterClass(parseExpression()));
      lexicator->advance();
      parseParamList();
    }
  }
  return paramVector;
}

如果您想要递归地构建列表(向量等),请使用以下模式:

private:
    void InternalBuild(std::vector<OutputData> & vec)
    {
        // Add data to vec
        // Possibly call recursively InternalBuild(vec);
    }
public:
    std::vector<OutputData> RecursiveBuild()
    {
        std::vector<OutputData> vec;
        InternalBuild(vec);
        return vec;
    }

值得注意的是,您在这里似乎滥用了递归。递归是指用于数据结构,这些数据结构的性质是递归的(树、图等)。在这种情况下,你可以递归地处理线性数据-为什么不简单地写一些东西呢:

while (!lexer.endOfExpression())
{
    // Process token
    lexer.Advance();
}

在您的情况下,如果表达式的长度足够长,则会出现堆栈溢出异常,因为您的程序将耗尽堆栈内存。如果你线性地实现这个算法,就不会发生这种情况。

试试这个,它会附加递归调用的结果

    std::vector<ParameterClass> recursiveParser :: parseParamList()
 {
std::vector<ParameterClass> paramVector;
if(lexicator->getCurrentToken()->getTokenType() == STRING)
{
    paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
    lexicator->advance();
    std::vector<ParameterClass> result = parseParamList();
    std::copy(result.begin(), result.end(), std::back_inserter(paramVector));
}
else
    if(lexicator->getCurrentToken()->getTokenType() == ID)
    {
        paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
        lexicator->advance();
        std::vector<ParameterClass> result = parseParamList();
        std::copy(result.begin(), result.end(), std::back_inserter(paramVector));
    }else
    {
        // so as to not fail in Expression, i need to check to see that there is a left paren indicating that there should be an expression
        if(lexicator->getCurrentToken()->getTokenType() == LEFT_PAREN)
        {
            paramVector.push_back(ParameterClass(parseExpression()));
            lexicator->advance();
            std::vector<ParameterClass> result = parseParamList();
            std::copy(result.begin(), result.end(), std::back_inserter(paramVector));
        }
    }
return paramVector;
 }

也请使用开关而不是if-elseif-elseif,避免重复代码

std::vector<ParameterClass> recursiveParser :: parseParamList()
{
    std::vector<ParameterClass> paramVector;
    switch(lexicator->getCurrentToken()->getTokenType())
    {
    case STRING:
        paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
        break;
    case ID:
        paramVector.push_back(ParameterClass(*lexicator->getCurrentToken()));
        break;
    case LEFT_PAREN: // so as to not fail in Expression, i need to check to see that there is a left paren indicating that there should be an expression
        paramVector.push_back(ParameterClass(parseExpression()));
        break;
    }
    lexicator->advance();
    std::vector<ParameterClass> result = parseParamList();
    std::copy(result.begin(), result.end(), std::back_inserter(paramVector));
    return paramVector;
}

但是使用@Spook的模式,因为它允许尾调用优化

相关内容

  • 没有找到相关文章

最新更新