In: Computer Science
Write a program in c++ using a map to create the following output.
Here is the list of students:
100: Tom Lee
101: Joe Jones
102: Kim Adams
103: Bob Thomas
104: Linda Lee
Enter a student an ID to get a student's name:
ID: 103
students[103] - Bob Thomas
# of students: 5
Delete a student (Y or N)? Y
Enter ID of student to be deleted: 103
Here is the list of students after the delete:
100: Tom Lee
101: Joe Jones
102: Kim Adams
104: Linda Lee
*/
#include<iostream>
#include<iterator>
#include<string.h>
#include<map>
using namespace std;
int main()
{
int temp,count=0;
char ch;
map<int,string> students;
students.insert(pair<int,string>(100,"Tom Lee"));
students.insert(pair<int,string>(101,"Joe Jones"));
students.insert(pair<int,string>(102,"Kim Adams"));
students.insert(pair<int,string>(103,"Bob Thomas"));
students.insert(pair<int,string>(104,"Linda Lee"));
map<int,string>::iterator itr;
cout<<"\n The list of students is:\n";
for(itr=students.begin();itr!=students.end();++itr){
cout<<"\t"<<itr->first<<"\t"<<itr->second<<"\n";
count+=1;
}
cout<<"Enter the student id to get the student
name"<<endl;
cout<<"ID :";
cin>>temp;
cout<<"students["<<temp<<"]-
"<<students.lower_bound(temp)->second<<endl;
cout<<"# of students: "<<count<<endl;
cout<<"delete a student (Y or N)? ";
cin>>ch;
if(ch=='Y')
{
cout<<"Enter the ID of student to be deleted : ";
cin>>temp;
students.erase(temp);
cout<<"\n Here is the list of students after the
delete:\n";
for(itr=students.begin();itr!=students.end();++itr){
cout<<"\t"<<itr->first<<"\t"<<itr->second<<"\n";
}
}
}
Output: