【问题标题】:How to make a recursive function that returns a vector in C++?如何在 C++ 中创建一个返回向量的递归函数?
【发布时间】:2021-12-13 13:31:56
【问题描述】:

我正在尝试将 python 函数转换为 c++,但没有成功。有人可以帮我吗?

python 函数接收一个字符串 S 和 2 个整数(fragment_size 和 jump)作为输入。 该函数的目的是将字符串 S 切成长度等于输入给定的第一个整数 (fragment_size) 的多个片段,并以等于输入给定的第二个整数的步长遍历整个字符串 S (jump )。

import sys
# First we read the input and asign it to 3 different variables
S = sys.stdin.readline().strip()
fragment_size = sys.stdin.readline().strip()
jump = sys.stdin.readline().strip()

def window(S, fragment_size, jump):
    word = S[:fragment_size]
    if len(word)< fragment_size:
        return []
    else:
        return [word] + window(S[jump:], fragment_size, jump)

# We check that S is not an empty string and that fragment_size and jump are bigger than 0. 
if len(S) > 0 and int(fragment_size) > 0 and int(jump) > 0:
    # We print the results 
    for i in window(S, int(fragment_size), int(jump)):
        print(i)

例如: 输入

ACGGTAGACCT
3
1

输出

ACG
CGG
GGT
GTA
TAG
AGA
GAC
ACC
CCT

示例 2: 输入

ACGGTAGACCT
3
3

输出

ACG
GTA
GAC

我知道如何在 c++ 中在窗口函数中返回一个字符串来解决这个问题。但我确实需要返回一个列表,就像我在 python 程序中返回的那样。

现在,我有这个 C++ 代码(不能编译):

# include <iostream>
# include <vector>
# include <string>
using namespace std;

vector<string> window_list(string word, vector<vector<string>>& outp_list){
    outp_list.push_back(word);
}

vector<string> window(string s, int len_suf, int jump){
    string word;
    vector<vector<string>> outp_list; 

    word = s.substr(0, len_suf);
    if(word.length() < len_suf){
        return vector<string> window_list();
    }
    else {
        window_list(word, outp_list);
        return window(s.substr(jump), len_suf, jump);
    } 
}

int main(){
    // We define the variables
    string s;
    int len_suf, jump;
    // We read the input and store it to 3 different variables
    cin >> s;
    cin >> len_suf;
    cin >> jump;
    // We print the result
    vector<string> ans = window(s, len_suf, jump);
    for(auto& x: ans){
        cout << x << endl;
    }

    return 0;
}

【问题讨论】:

  • vector&lt;string&gt; window_list() 是一个函数定义。您可能只想return vector&lt;string&gt;();
  • else 分支中,即使您忽略返回的向量,您也可以使用正确的参数正确调用window_list。但是在另一个分支中,您的调用无效,类型介于 return 和调用之间,并且您没有将任何参数传递给调用。在这种情况下,您真的应该调用 window_list 函数,还是返回一个空向量?
  • @Some 程序员老兄,我只是想返回一个空向量,就像在 python 程序中一样。但是我刚开始使用C++,我真的不知道该怎么做。
  • @UnholySheep,好的,我会努力的!谢谢
  • 对于“刚开始使用 C++”的人来说,这个程序似乎太复杂了。或许您应该退后一步,投资some good books 并先了解更多基础知识。

标签: python c++ recursion vector


【解决方案1】:

您可以如below 示例所示简化程序:


#include <iostream>
#include <vector>
#include <string>
std::vector<std::string> stringFragmenter(const std::string &inputString, int len, int stepSize)
{
    std::string::const_iterator currentBegin = inputString.begin();
    
    std::string::const_iterator end = inputString.end();
    std::string::const_iterator currentPosition = currentBegin;
    
    std::vector<std::string> output;
    std::string pushedString;
    while(currentPosition + len <= end)
    {
        currentBegin = currentPosition;
        std::string::const_iterator currentEnd = currentBegin + len;
        
       
        
        
        while(currentBegin != currentEnd)
        {
            pushedString+= *currentBegin;
            ++currentBegin;
        }
        output.push_back(pushedString);
        
        currentPosition = currentPosition + stepSize;
        
        //clear the pushedString for next iteration
        pushedString.clear();
        
    }
    return output;
}
int main()
{
    std::string inputString;
    std::cin >> inputString;
    
    int length;//this denotes the length of string that has to be displayed(stored) at each time
    std::cin >> length;
    
    int stepSize;
    std::cin >>stepSize;//this denotes the jump size
    
    
    
    std::vector<std::string> fragmentedString = stringFragmenter(inputString, length, stepSize);
    
    //print out the elements of the returned vector so that we can confirm if it contains the correct elements
    for(const std::string &elem: fragmentedString)
    {
        std::cout<<elem<<std::endl;
    }

    
    return 0;
}


here 所示,上述程序的输出与您的两种输入情况均匹配。

在上述程序中,要输入您的输入 case 1,您应该首先输入字符串 ACGGTAGACCT,然后输入长度 3,最后输入 stepSize(或跳转大小)1,输入/输出如下所示:

ACGGTAGACCT
3
1
ACG
CGG
GGT
GTA
TAG
AGA
GAC
ACC
CCT

与您想要的输出相匹配。对于您的输入案例 2,同样如此。查看输出 here

【讨论】:

    猜你喜欢
    • 2020-09-20
    • 2014-05-22
    • 2014-01-22
    • 1970-01-01
    • 2022-01-26
    • 2013-10-10
    • 1970-01-01
    • 2023-03-14
    • 2018-07-15
    相关资源
    最近更新 更多