6-4. Modify the movie list so it is now a set of movies. After the sentinel loop
ID: 3776209 • Letter: 6
Question
6-4. Modify the movie list so it is now a set of movies. After the sentinel loop to fill up the set, let the user insert or delete by movie name (but do a check to see if it is already in the set (the elements must be unique for the insert). The set will already be sorted, but then use the copy & iterator to print on the screen.
The following is ex6-4a.cpp with comments showing the missing lines of code. Fill in the missing lines of code for part b - ex6-4b.cpp.
/*
Exercise 6-4a
This program will create a movie set
*/
#include<iostream>
#include<set>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
void main()
{
string name;.
int ans, pos;
bool found = false;
// declare set of strings vm
// declare set iterator vmit
// declare iterator to cout osit
// declare pair of set of string iterator named p
cout << "Enter movie name (-1 to end): ";
getline(cin, name, ' ');
while (name != "-1")
{
// insert name into set, assign to pair object
// if pair's second field is true
// print that pair's first field was inserted
else
// print that pairs' first field was already in set
cout << "Enter movie name (-1 to end): ";
getline(cin, name, ' ');
}
cout << "Enter 1 to insert, 2 to delete: ";
cin >> ans;
if (ans == 1)
{
cout << "Enter movie name: ";
cin.ignore(1, ' ');
getline(cin, name, ' ');
// insert name into set, assign to pair object
// if pair's second field is true
// print that pair's first field was inserted
else
// print that pairs' first field was already in set
}
else if (ans == 2)
{
cout << "Enter movie name to delete: ";
cin.ignore(1, ' ');
getline(cin, name, ' ');
// find name in set, return to iterator
// if iterator is not at the end of set
{
// print what iterator is pointing at to say will be deleted
// delete that name from set
}
else
cout << name << " not in set ";
}
cout << "Number of movies in set is " // print size
// copy set to cout iterator
}
Explanation / Answer
/*
Exercise 6-4a
This program will create a movie set
*/
#include<iostream>
#include<set>
#include<string>
#include<iterator>
#include<algorithm>
using namespace std;
int main()
{
string name;
int ans, pos;
bool found = false;
std::set<std::string> movieSet;
std::set<std::string>::iterator strIt;
std::pair<std::set<std::string>::iterator,bool> strRet;
std::cout << "Enter movie name (-1 to end): ";
getline(cin, name, ' ');
while (name != "-1")
{
// insert name into set, assign to pair object
strRet = movieSet.insert(name);
// if pair's second field is true
// print that pair's first field was inserted
if (strRet.second==true) {
std::cout << name << " was inserted" << endl;
}
else{
// print that pairs' first field was already in set
std::cout << name << " was already in set" << endl;
}
std::cout << "Enter movie name (-1 to end): ";
getline(cin, name, ' ');
}
std::cout << "Enter 1 to insert, 2 to delete: ";
std::cin >> ans;
if (ans == 1)
{
std::cout << "Enter movie name: ";
std::cin.ignore(1, ' ');
getline(cin, name, ' ');
// insert name into set, assign to pair object
strRet = movieSet.insert(name);
// if pair's second field is true
// print that pair's first field was inserted
if (strRet.second==true) {
std::cout << name << " was inserted" << endl;
} else {
// print that pairs' first field was already in set
std::cout << name << " was already in set" << endl;
}
}
else if (ans == 2)
{
std::cout << "Enter movie name to delete: ";
cin.ignore(1, ' ');
getline(cin, name, ' ');
// find name in set, return to iterator
strIt = movieSet.find(name);
// if iterator is not at the end of set
if(strIt != movieSet.end())
{
// print what iterator is pointing at to say will be deleted
std::cout << name << " will be deleted" << endl;
// delete that name from set
movieSet.erase(strIt);
}
else
std::cout << name << " not in set ";
}
// copy set to cout iterator
std::cout << "Number of movies in set is " << movieSet.size() << endl; // print size
for(strIt = movieSet.begin(); strIt != movieSet.end(); ++strIt)
{
std::cout << ' ' << *strIt;
}
std::cout << ' ';
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.