*Please utilize C++ format when answering questions 42. a) Write a function that
ID: 3626483 • Letter: #
Question
*Please utilize C++ format when answering questions42. a) Write a function that passes in a vector of strings and a single string value and returns true if that string value exists in the vector and false if it doesn’t. For example, if the vector contains the values, “Kris”, “Joan”, “Sharon”, “Luke” and the single string value passed in was “Luke”, then the function should return true. However, if the single string value passed in was “Lynn”, then the function should return false.
b) Write a main function that tests this function at least once. For the vector, you may
initialize it with any values you choose, but you must give it at least 2 values.
Explanation / Answer
please rate - thanks
#include <iostream>
#include <vector>
using namespace std;
bool compare2(vector<string>,string);
int main()
{vector <string> a,b;
int n;
string s;
cout<<"how many elements in the vector: ";
cin>>n;
while(n<2)
{cout<<"must be at least 2 how many elements in vector a: ";
cin>>n;
}
for(int i=0;i<n;i++)
{cout<<"enter a string: ";
cin>>s;
a.push_back(s);
}
cout<<"enter a string to search for: ";
cin>>s;
if(compare2(a,s))
cout<<s<<" is in the vector"<<endl;
else
cout<<s<<" is not in the vector"<<endl;
system("pause");
return 0;
}
bool compare2(vector<string> a,string b)
{
for(int i=0;i<a.size();i++)
if(a[i].compare(b)==0)
return true;
return false;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.