Can anyone help me convert this to C++ but instead of using an array using a vec
ID: 3707639 • Letter: C
Question
Can anyone help me convert this to C++ but instead of using an array using a vector with 12 elements?
Can anyone help me convert this to C++ but instead of using an array using a vector with 12 elements?
Creates a new NameSurferEntry from a data 1ine as it appears in the data file. Each line begins with the name, which is followed by integers giving the rank of that name for each *decade. o g public NameSurferEntry (String line) { String] splitResult - new String[12]; 1/ 12 elements including the name splitResult -line.split(" "); name splitResult[e]; for (int i-1; iExplanation / Answer
Hi,
we can convert each line of this java to the respective c++ language. But there is one problem in c++ string library there is no default split function which will take a string and split it by the passed delimiter. So, we have to write our own split function here the code that is converted to c++ and split function is also included.
Things to observe before going to the program:
here in the existing Java code, the array ranks is not initialized. Because we are converting a string to the integer and appending it to the ranks array we can assume that the ranks array is initialized before the function call as a global variable and of type integer.
And in c++ there no need to specify a vector's length so in the code the split functions return value is directly stored and since we know that the file contains only 12 years data for loop i value we can directly use 12.
In c++ the Integer.parseInt() direct function is not there.
we have to define a string input stream and then we have to stream that value to an integer.
vector<string> splitStrings(string str, char dl)
{
string word = "";
int num = 0;
str = str + dl;
int l = str.size();
vector<string> substr_list;
for (int i = 0; i < l; i++) {
if (str[i] != dl)
word = word + str[i];
else {
if ((int)word.size() != 0)
substr_list.push_back(word);
word = "";
}
}
return substr_list;
}
int NameSurferEntry(String line){
char de=' ';
//calling the split function with delimiter ' '(space).
vector<string> splitResult=splitStrings(line,de);
name=splitResult[0];
for(int i=1;i<12;i++){
//defining string stream for the value we need to convert into the integer
stringstream converter(splitResult[i]);
//streaming the integer value to integer variable defined before
converter>>ranks[i-1];
}
return 0;
}
Please comment below if you need any other clarification.
vector<string> splitStrings(string str, char dl)
{
string word = "";
int num = 0;
str = str + dl;
int l = str.size();
vector<string> substr_list;
for (int i = 0; i < l; i++) {
if (str[i] != dl)
word = word + str[i];
else {
if ((int)word.size() != 0)
substr_list.push_back(word);
word = "";
}
}
return substr_list;
}
int NameSurferEntry(String line){
char de=' ';
//calling the split function with delimiter ' '(space).
vector<string> splitResult=splitStrings(line,de);
name=splitResult[0];
for(int i=1;i<12;i++){
//defining string stream for the value we need to convert into the integer
stringstream converter(splitResult[i]);
//streaming the integer value to integer variable defined before
converter>>ranks[i-1];
}
return 0;
}