I am trying to translate a python function to c++ without success. Can someone help me?
The python function receives as input a string S and 2 integers (fragment_size and jump). The aim of this function is to slice the string S in a number of fragments of length equal to the first integer given by the input (fragment_size) and traverse the whole string S with a step equal to the second integer given by the input (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)
For example: Input
ACGGTAGACCT
3
1
Output
ACG
CGG
GGT
GTA
TAG
AGA
GAC
ACC
CCT
Example 2: Input
ACGGTAGACCT
3
3
Output
ACG
GTA
GAC
I know how to solve this in c++ returning a string in the window function. But I really need to return a list, like the one I am returning in the python program.
Right now, I have this C++ code (that does not compile):
# 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;
}