Often some situation arises in programming where data or input is dynamic in nature, i.e. the number of data item keeps changing during program execution. A live scenario where the program is developed to process lists of employees of an organization. The list grows as the names are added and shrink as the names get deleted. With the increase in name the memory allocate space to the list to accommodate additional data items. Such situations in programming require which technique. Explain the concept with the help of suitable examples.
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<string, int> names;
bool menu = true;
while(menu)
{
cout << "Choose the operation(1, 2): " << endl;
cout << "1. Add name to vector" << endl;
cout << "2. Remove name from vector" << endl;
cout << "3. Exit" << endl;
int choice;
int counter = 0;
cin >> choice;
switch(choice);
{
case 1:
counter++;
string name;
cout << "Enter name you want to add: ";
cin >> name;
names[name] = counter;
break;
case 2:
cout << "Enter name you want to remove: ";
cin >> name;
names.erase(name);
break;
}
}
return 0;
}
Comments
Leave a comment