In: Computer Science
Write a function in c++, called afterAll that takes two parameters, a vector of string and a string. The function returns true if the 2nd parameter comes after all of the strings in the vector, order-wise, false if not. As an example, "zoo" comes after "yuzu".
In this function afterAll(), we have to check if the second string parameter lexically comes after all the strings in the first vector parameter .
To do this, let's loop over each string in the vector. If the string is lexically greater the string in second argument, then return false.
If the function completes the loop without returning false, then return true
function:
bool afterAll(vector<string> s1, string s2){
for(auto s: s1){
if(s>s2)
return
false;
}
return true;
}
sample program to test the function
I am writing a sample program to test this function. I have a vector with elements "me","men", and "meds". And I will be using "met" and "meg" as second parameters. The first function call should return true, and the second one must return false.
So, here is the program:
#include <iostream>
#include <vector>
using namespace std;
bool afterAll(vector<string> s1, string s2){
for(auto s: s1){
if(s>s2)
return
false;
}
return true;
}
int main(){
vector<string> v;
v.push_back("me");
v.push_back("men");
v.push_back("meds");
cout<<afterAll(v,"met")<<endl;
cout<<afterAll(v,"meg")<<endl;
}
output: