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.
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...
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.
Write a C function str_to_float() that takes a numeric string as input and converts it to...
Write a C function str_to_float() that takes a numeric string as input and converts it to a floating point number. The function should return the floating point number by reference. If the conversion is successful, the function should return 0, and -1 otherwise (e.g. the string does not contain a valid number). The prototype of the function is given below int str_to_float(char * str, float * number);
Write a function called printChList that takes as its parameters a character array, its size, and...
Write a function called printChList that takes as its parameters a character array, its size, and output file stream. The function should print the contents of the array to the output file. code with c++ and detail explaination
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and...
Write a C++ function, smallestIndex, that takes as parameters an int array and its size and returns the index of the first occurrence of the smallest element in the array. Write another C++ function,lastLargestIndex, that takes as parameters an int array and its size and returns the index of the last occurrence of the largest element in the array. An analysis and design of the function smallestIndex is given below. Write an analysis and design for the function lastLargestIndex. Write...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT