I\'m having trouble figuring out how to remove elements from avector. Here is th
ID: 3610393 • Letter: I
Question
I'm having trouble figuring out how to remove elements from avector. Here is the specific problem:The member function whosLeft takes as input a vector of stringsnames and an integer k, and it returns the string that correspondsto the last element of names that is removed by the followingprocess. To begin, we start at the first element of the vector, andwe start counting. When we reach the kth element, we remove it.Starting at the next element, we again start counting. When we gopast the end of the
vector, we start over again at the first element. When we reach thekth element again, we remove it. This process continues until thereis exactly one element left in the array.
For example, if k is equal to 3 and the vector names contains thestrings Joe, Bob, Kathy, Larry, and Sandy, then the first elementremoved is Kathy. The next element removed is Joe. After that,Sandy is removed. Finally, Bob is removed. So, the last onestanding is Larry.
The prototype for the function is as follows:
string whosLeft(vector<string> names, int k);
Explanation / Answer
str_Vector.erase(str_Vector.begin()+1,str_Vector.begin()+2);
I hope it is useful toyou.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.