GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

Tuesday, March 8, 2011

CPP - Map Assoc Containers

#include <iostream>
#include <cstdlib>
#include <map>

using namespace std;

int main()
{
    system("color 1f");

    map<char, char*> data;
    map<char, char*>::iterator it;

    // inserting some data
    data.insert(pair<char, char*>('a',"Luna Maya"));
    data.insert(pair<char, char*>('b',"Lady Gaga"));
    data['c'] = "Bill Clinton";

    // iterating the data
    // output key value pairs
    for( it = data.begin(); it != data.end(); it++){
        cout << (*it).first << " = " << (*it).second << endl;
    }

    // removing some data
    data.erase('b');

    // getting to know the size of the elements
    cout << data.size() << endl;

    // output the data
    cout << data['a'] << endl;
    cout << data['c'] << endl;

    return 0;
}

Share/Bookmark

No comments:

Post a Comment