Wed Dec 25 2019

Map

C++ Programming1947 views

File Name: map.cpp

#include<iostream>
#include<map>
using namespace std;

int main() {

	/* Declare map */
	map <string, float> item;

	/* Map iterator */
	map <string, float> :: iterator p;

	/* Initialize map */
	item["Eraser"] = 0.50;

	/* Insert data */
	item.insert(pair<string, float>("Pen", 2.50));
	item.insert(pair<string, float>("Book", 200.55));
	item.insert(pair<string, float>("Pencil", 1.00));
	item.insert(pair<string, float>("Ex-Book", 250.10));

	string srch = "Book";

	/* Search data */
	p = item.find(srch);
	if(p != item.end())
		cout << "Price of the " << srch << " is " << p->second << endl;
	else
		cout << "Item not found!" << endl;

	/* Erase data */
	item.erase("Pencil");
	cout << "Item Deleted!" << endl;

	/* Display all elements */
	for (p = item.begin(); p != item.end(); p++) 
		cout << "Item: " << p->first << "\t"<< "Price: " << p->second << endl;

	return 0;
}


/* Output */
Price of the Book is 200.55
Item Deleted!
Item: Book Price: 200.55
Item: Eraser Price: 0.5
Item: Ex-Book Price: 250.1
Item: Pen Price: 2.5
Reference:

We use cookies to improve your experience on our site and to show you personalised advertising. Please read our cookie policy and privacy policy.