This is java code ... ...assuming I have a vectorlist first, then later, initiat
ID: 3848318 • Letter: T
Question
This is java code
...
...assuming I have a vectorlist first, then later, initiate a vectorsubList (explaining the meanings of subList and list in code)
int index = 0; //explains where index came from
Char is the name of a previously created class
BigDecimal is the name of the class the vectors are within...
...
What does this mean in C++ code (what does it look like after being converted into c++)?
...
...
...
Also, what is return Integer.parseInt(s);
translated in C++?
subList = this.list.subList(0,index);Explanation / Answer
For the first part let us assume that you have already initiated both vectors vectorList and vectorSubList and and you just need to initiate values of vectorSubList from vector by from 0 to a particular index.
That means suppose vectorList is having 8 elements and you need something like first 5 elements ... i.e. your index is 4 and you need elements from indec 0 to 4 from vectoList to vectorSubList.
There is a method subList in c++ STL vector class and vector.h header file the definition of the method is as follows:
The method subList generates a new subList from vector list v from position start to length given .. i.e. the call of subList(2, 4) would return a new vector containing elements 2-5 of the original vector.( Here 4 means length which starts from 1 , so here elements extracted are 4 elements, i.e. 2,3,4,5 indexed elements.)
So for our code statement which is in java as follows: -
subList = this.list.subList(0,index);
we can convert this using above function as follows: -
vectorSubList = this->vectorList.subList(0,(index+1));
(Here index + 1 is used because, suppose we need to extract list from 0 to index 4, so we need 5 elements, i,e, 0,1,2,3,4, and out method expects length i.e. number of elements, So we need to give 5 here, so I added index + 1 in place of index.)
2.
return Integer.parseInt(s);
for this one , we have basically many ways to do that, but most common are as follows: -
a) std::stoi
This one is basically available in most of the latest versions of c++ and works perfectly with c++ 11.We need to include string.h for this one. the converted statement in this is as follows: -
return stoi(s);
b) int atoi(const char* str)
This one is present in standard library, the problem with this one is that it has poor failure detection and it accepts null terminated string char. So converted statement is: -
return std::atoi(s.c_str());
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.