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

I need help with a C++ program involving #include<set> The purpose of this lab i

ID: 3862204 • Letter: I

Question

I need help with a C++ program involving #include<set>

The purpose of this lab is to gain some familiarity with an STL Container, and the use of an external iterator. For this lab you will NOT be creating any classes of your own. In fact you can do the whole lab in the main, although I think that you will see that it is highly advisable to have at least one non-member function since there is a task that is repeated.

At the top of your program #include <set> as well as iostream, string, and fstream. Declare a multiset that is capable of holding strings.

Fill the multiset with all the names in the file. (Since there are no spaces in the names you can use >> or getline.)

Now declare an iterator that is appropriate for traversing the multiset. Set it to the beginning of the multiset full of names, and walk through the list, outputting each name it finds. (There are a lot of them, so you may want to separate them with spaces instead of putting one on each line.)

Run and test this program. Your name should be somewhere in the list. (Note that the names are now in alphabetical order.)

Here is the place where you want to pass your STL container to a non-member function, since we are doing something that is basically the same three times in a row. If you do you should pass the container by const and reference, since it is large. You will probably want one additional parameter.

Declare two iterators.

Move one of them through the list to the first name that begins with ‘M’. Since you know *it is returning a string, you will know that (*it).at(0) will return the first character in that string.

Now set the second iterator equal to the first. Use it to count how many M names there are in the list. Print this number out.

Using the second iterator, print out all the M names in reverse alphabetical order. (Remember that these are bidirectional iterators, so you --it works to go backwards.)

Now do the above three steps for all the ‘C’ names and then for all the ‘Q’ names.

If I had asked for ‘Y’ (I didn’t) the output would look like:

There are 3 Y names in the list

They are:Yujia Yuanhang Yingjie

This program has no user interaction. When you have it working, simply start a script file, run the program once, and close the script file.

---------------------------------------------------

names .txt below

----------------------------------------------------

Kevin
Billy
William
Ali
Colleen
Jessica
Matthew
Samantha
Chris
Benjamin
Casey
Meghan
Evan
Lydia
Alex
Nicholas
Luke
Nicholas
Waylon
Robert
Jeffrey
Zachary
Hanna
Dillon
Nicholas
Michael
Robby
Geoffrey
Kelly
Joshua
Jordan
Damian
Samuel
David
David
Jessie
Nick
Jordan
Adam
Spencer
Joseph
Matthew
Dallas
Samuel
David
Levi
Jacob
Austin
Zachary
Matthew
Mick
Gerrod
Zachary
Michael
Luke
Alexander
Doug
Anthony
Aaron
Andrew
Charles
Michael
Elita
Max
Daniel
James
Shawn
Shawn
Sergio
Tyrell
Matthew
Ana
Abdullah
Jack
Brian
Bo
Eric
Channing
Kevin
Hugh
Serge
Max
Kyle
Matthew
Cameron
Kerby
Derek
Paul
Brandon
Taffie
Jacob
Jacaria
Christina
Michael
Kit
Uriah
Minyuan
Alexander
Kyle
Patrick
Nathan
Jeromy
Elaine
Matthew
Cody
Marilyn
Keenan
Brady
Matthew
Brandon
Jose
Andrew
Benjamin
Weston
Gregg
Brian
Sam
Eric
Robert
Daniel
Jessi
Joseph
Kellie
Joshua
Jared
Kevin
Jason
Robbie
Adam
Christian
Joseph
Michael
Zane
Alex
Brady
Patricia
Michael
Kyle
Joseph
Joseph
Jessi
Daniel
Nathaniel
Douglas
Dj
Jonathan
Trevor
Lu
Sean
Michael
Zach
Natalie
Derrick
Jacob
Edward
Harrison
Andy
Justin
Jared
Yujia
Gabriel
Luke
Channing
Gregory
Hannah
Ashleigh
Carson
Zane
Oliver
Eliza
Derek
Levi
Andrew
Matthew
William
Mohamed
Catherine
Emily
Eric
Tyler
Thomas
Nathan
Matthew
Jacob
John
Kathy
Brian
Tyler
Aaron
Vitalik
Alex
Nathan
Dylan
Matthew
Trey
Ryan
Brock
Roger
Jarod
Kevin
Aaron
Michael
Cira
Jacob
Yingjie
Philip
Benjamin
Eric
Bryan
Joel
Mingda
Frank
Kristen
Jacob
Joe
Jackson
Justin
Keon
Favour
Kevin
Hannah
Christopher
Abhilash
Aaron
Nathan
Dudley
Michael
Selena
David
Caitlin
David
Ryan
Trent
Zachary
Matthew
Andrew
Dean
Ke
Mollie
Payge
Yuanhang

Explanation / Answer

#include <iostream>
#include <set>
#include<string>
#include<fstream>
using namespace std;
int main()
{
   ifstream in;
   std::set<string> namesContainer;
   //iterator
   std::set<string>::iterator it1,it2;
   string s;
   //open file
   in.open("names.txt");
   if (!in)
   {
       cout << "name.txt cannot be opened" << endl;
   }
   while (!in.eof())
   {
       in >> s;
       namesContainer.insert(s);
   }
  

   //display names
   int count = 0;
   cout << "Names read from file are: ";
   for (it1 = namesContainer.begin(); it1 != namesContainer.end();it1++)
   {
       if ((*it1).at(0) == 'M')
       {
           it2 = it1;
           count++;
          
       }
       cout << *it1 << endl;
   }
   //print names beginning with 'M' in reverse alphabetical order.
   cout << " There are " << count << " names beginning with 'M': ";
   for (int i = count-1; i >=0; i--,it2--)
   {
       cout << ' ' << *it2;
   }
   cout << endl;
   //find the names begin with C
   count = 0;
  
   for (it1 = namesContainer.begin(); it1 != namesContainer.end(); it1++)
   {
       if ((*it1).at(0) == 'C')
       {
           it2 = it1;
           count++;

       }
   }
   //print names beginning with 'C' in reverse alphabetical order.
   cout << " There are " << count << " names beginning with 'C': ";
   for (int i = count - 1; i >= 0; i--, it2--)
   {
       cout << ' ' << *it2;
   }
   cout << endl;
   //find the names begin with Q
   count = 0;
   cout << " There are "<<count<<" names beginning with 'Q': ";
   for (it1 = namesContainer.begin(); it1 != namesContainer.end(); it1++)
   {
       if ((*it1).at(0) == 'Q')
       {
           it2 = it1;
           count++;

       }
   }
   //print names beginning with 'Q' in reverse alphabetical order.
   for (int i = count - 1; i >= 0; i--, it2--)
   {
       cout << ' ' << *it2;
   }

   cout << endl;
  
}

---------------------------------------------------------------------------------------

//output

Names read from file are: Aaron
Abdullah
Abhilash
Adam
Alex
Alexander
Ali
Ana
Andrew
Andy
Anthony
Ashleigh
Austin
Benjamin
Billy
Bo
Brady
Brandon
Brian
Brock
Bryan
Caitlin
Cameron
Carson
Casey
Catherine
Channing
Charles
Chris
Christian
Christina
Christopher
Cira
Cody
Colleen
Dallas
Damian
Daniel
David
Dean
Derek
Derrick
Dillon
Dj
Doug
Douglas
Dudley
Dylan
Edward
Elaine
Elita
Eliza
Emily
Eric
Evan
Favour
Frank
Gabriel
Geoffrey
Gerrod
Gregg
Gregory
Hanna
Hannah
Harrison
Hugh
Jacaria
Jack
Jackson
Jacob
James
Jared
Jarod
Jason
Jeffrey
Jeromy
Jessi
Jessica
Jessie
Joe
Joel
John
Jonathan
Jordan
Jose
Joseph
Joshua
Justin
Kathy
Ke
Keenan
Kellie
Kelly
Keon
Kerby
Kevin
Kit
Kristen
Kyle
Levi
Lu
Luke
Lydia
Marilyn
Matthew
Max
Meghan
Michael
Mick
Mingda
Minyuan
Mohamed
Mollie
Natalie
Nathan
Nathaniel
Nicholas
Nick
Oliver
Patricia
Patrick
Paul
Payge
Philip
Robbie
Robby
Robert
Roger
Ryan
Sam
Samantha
Samuel
Sean
Selena
Serge
Sergio
Shawn
Spencer
Taffie
Thomas
Trent
Trevor
Trey
Tyler
Tyrell
Uriah
Vitalik
Waylon
Weston
William
Yingjie
Yuanhang
Yujia
Zach
Zachary
Zane

There are 10 names beginning with 'M': Mollie Mohamed Minyuan Mingda Mick Micha
el Meghan Max Matthew Marilyn

There are 14 names beginning with 'C': Colleen Cody Cira Christopher Christina
Christian Chris Charles Channing Catherine Casey Carson Cameron Caitlin

There are 0 names beginning with 'Q':

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote