C++ Programming

Map

Learn c plus plus by example of map and its usage

12/25/2019
0 views
map.cppCPP
#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
cppc plus plusmap

Related Examples

Mashable is a global, multi-platform media and entertainment company For more queries and news contact us on this
Email: info@mashablepartners.com