C++
/* Write code for void function display_vector that accepts parameter const reference vector of strings. The function will iterate through the vector and display a string per line. */
Write code for void function update_vector_element that accepts parameter reference vector of strings, a string vector search_value, and a string replace_value. The function will iterate through the vector and search for the search_value and if found will replace the vector element with the replace_value. */
Write code to create a vector of string names, add the values "John", "Mary", "Patty",
"Sam" to the vector, call the function display_vector and pass the names vector as
an argument, prompt user for a name to search for and a value to replace searched value with.
Afterward, call the update_vector_element with the names vector and call the display_vector function.
Don't worry about displaying a message for values that aren't found. Assume user will give you a valid name.
Following is the C++14 code.
#include <bits/stdc++.h>
using namespace std;
void display_vector(const vector<string> & sv)
{
vector<string>::const_iterator it;
for (it = sv.begin(); it != sv.end(); ++it) {
cout << " " << *it;
}
cout << endl;
}
void update_vector( vector<string> & sv, string
search, string replace)
{
vector<string>::iterator it;
for (it = sv.begin(); it != sv.end(); ++it) {
if ( *it == search) {
*it = replace;
break;
}
}
}
int main()
{
vector<string> names;
names.push_back("John");
names.push_back("Mary");
names.push_back("Patty");
names.push_back("Sam");
display_vector(names);
string ss, rs;
cout << "Enter the search string : " ;
cin >> ss;
cout << "Enter the replace string : ";
cin >> rs;
update_vector(names, ss, rs);
display_vector(names);
return 0;
}
Output of program execution:
John Mary Patty Sam
Enter the search string : Patty
Enter the replace string : Peter
John Mary Peter Sam
C++ /* Write code for void function display_vector that accepts parameter const reference vector of strings....