Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a text file of names, with one name per line, that has been compiled fr

ID: 3634951 • Letter: C

Question

Consider a text file of names, with one name per line, that has been compiled from several different sources. A sample follows:


Michael Joe
Edward Jacob
Ramon Ezra
Sean Nakamura
Michael Joe
Jie Lee
Meguro Sam
Edward Jacob
David Johnny


There are duplicate names in the file. We would like to generate an invitation list for a holiday party, but don’t want to send multiple invitations to the same person. Write a program that eliminates the duplicate names by using the set template class. Read each name from the file, add it to the set, then
output all names in the set to generate the invitation list without duplicates.

Explanation / Answer

#include <iostream>
#include <set>
/*
using std::cout;
using std::endl;
using std::vector;
*/
using namespace std;


int main()
{

set<string> aSet;

aSet.insert("Michael Joe");
aSet.insert("Edward Jacob");
aSet.insert("Ramon Ezra");
aSet.insert("Sean Nakamura");
aSet.insert("Michael Joe");
aSet.insert("Jie Lee");
aSet.insert("Meguro Sam");
aSet.insert("Edward Jacob");
aSet.insert("David Johnny");
// changes are shown in red colour

cout << "The set contains:" << endl;
// note const_iterator, not iterator. This iterator cannot be
// used to make changes to the set
set<string>::const_iterator setItr;
for(setItr = aSet.begin(); setItr != aSet.end(); setItr++)
{
cout << *setItr << endl;
}
cout << endl;
return 0;
}