Question

In: Computer Science

Write a function in c++, called afterAll that takes two parameters, a vector of string and...

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".

Solutions

Expert Solution

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:


Related Solutions

C programming Write a function called string in() that takes two string pointers as arguments. If...
C programming Write a function called string in() that takes two string pointers as arguments. If the second string is contained in the first string, have the function return the address at which the contained string begins. For instance, string in(“hats”, “at”) would return the address of the a in hats. Otherwise, have the function return the null pointer. Test the function in a complete program that uses a loop to provide input values for feeding to the function.
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates...
Write a recursive Racket function "remove-char" that takes two string parameters, s and c, and evaluates to string s with all occurrences of c removed. The string c is guaranteed to be a length-1 string; in other words a single character string. For example (remove-char "abc" "b") should evaluate to "ac". Here is pseudocode that you could implement.
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters,...
This is an intro to python question. #Write a function called search_for_string() that takes two #parameters, a list of strings, and a string. This function #should return a list of all the indices at which the #string is found within the list. # #You may assume that you do not need to search inside the #items in the list; for examples: # # search_for_string(["bob", "burgers", "tina", "bob"], "bob") # -> [0,3] # search_for_string(["bob", "burgers", "tina", "bob"], "bae") # -> []...
In C++ Write a function which takes two parameters: an array of ints and an int...
In C++ Write a function which takes two parameters: an array of ints and an int size of the array and prints every element greater than 5 to the screen. As an example, if the array has the following 10 elements: 2 5 8 9 7 1 0 2 6 3, your function should print out 8 9 7 6. You may assume that the parameters passed to the function are valid. Your function must have the following signature: void...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters....
Sovle with python 3.8 please. 1, Write a function called same_without_ends that has two string parameters. It should return True if those strings are equal WITHOUT considering the characters on the ends (the beginning character and the last character). It should return False otherwise. For example, "last" and "bask" would be considered equal without considering the characters on the ends. Don't worry about the case where the strings have fewer than three characters. Your function MUST be called same_without_ends. You...
Program in C Write a function that takes a string as an argument and removes the...
Program in C Write a function that takes a string as an argument and removes the spaces from the string.
Programming in C (not C++) Write the function definition for a function called CompareNum that takes...
Programming in C (not C++) Write the function definition for a function called CompareNum that takes one doyble argument called "num". The function will declare, ask, and get another double from the user. Compare the double entered by the user to "num" and return a 0 if they are the same, a -1 num is less than the double entered by the user and 1 if it is greater.
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT