Question

C++ /* Write code for void function display_vector that accepts parameter const reference vector of strings....

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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

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



Add a comment
Know the answer?
Add Answer to:
C++ /* Write code for void function display_vector that accepts parameter const reference vector of strings....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT