Skip to content

Instantly share code, notes, and snippets.

@mubbashir10
Last active March 16, 2016 14:43
Show Gist options
  • Save mubbashir10/5e54b4887ed91faee70e to your computer and use it in GitHub Desktop.
Save mubbashir10/5e54b4887ed91faee70e to your computer and use it in GitHub Desktop.
A simple example of map usage (STL C++). Traverse, add, create maps in c++.
/*
About: A simple tutorial of maps (c++ STL)
Author: Mubbashir10
URL: http://mubbashir10.com
*/
//libraries
#include <iostream>
#include <string>
#include <map>
//defining namespace
using namespace std;
//main function
int main(){
//declaring map
map <int, string> data;
//adding elements
data[1] = "Mubbashir";
data[2] = "Samra";
data[3] = "Faiza";
data[4] = "Asfand";
data[5] = "Shabana";
//printing map
cout<<"Elements of data (via index) "<<endl;
cout <<data[1]<<endl;
cout <<data[2]<<endl;
cout <<data[3]<<endl;
cout <<data[4]<<endl;
cout <<data[5]<<endl;
//traversing map
cout<<"Elements of data (via iterator) "<<endl;
map<int,string>::iterator point;
point = data.begin();
while(point!=data.end()){
cout<<point->first;
cout<<" maps to ";
cout<<point->second<<endl;
point++;
}
//exiting
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment