Monday, December 21, 2009

STL Map Example

//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// STL Map Example
// Map operations Insert, Find, Iterate
// Date : 19 - Jun -2008
// Author: Prakhar Dubey (prakharprakhar@gmail.com)
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

#include <iostream>
#include <stdlib.h>
#include <map>
#include <string.h>
using namespace std;


typedef struct NODE{
int x;
char domain[64];
char *xdomain;
}NODE;

map<char *,NODE> mymap;
map<char *,NODE>::iterator it;
int main ()
{
NODE n1,n2,n3,n4;
n1.x= 1;
n2.x= 2;
n3.x= 3;
strcpy(n1.domain,"hello1");
strcpy(n2.domain,"hello2");
strcpy(n3.domain,"hello3");

char *str = "abcd";

mymap["abc"]=n1;
mymap["ab"] =n2;
mymap["abcd"] =n3;

it = mymap.find(str);
cout << "it = " << it->second.domain << endl;;

//print content:
cout << "elements in mymap:" << endl;
for ( it = mymap.begin(); it != mymap.end(); it++)
{
cout << "elements in mymapi is : "<< it->second.domain << " " <<it->first << endl;
}
return 0;
}

No comments:

Post a Comment