Monday, August 21, 2006

STL:Map

from metashell.com
#pragma warning(disable: 4786)


#include
#include
#include

using namespace std;

int main() {
map ass_array;

// Assign keys and values

ass_array["fat"] = 0;
ass_array["sodium"] = 45;
ass_array["totalcarb"] = 46;
ass_array["protien"] = 0;

// Insert Value

ass_array.insert( map::value_type( "potassium", 25 ) );

// Print value of sodium

cout << ass_array["sodium"] << endl;
cout << "The map has " << ass_array.size() << " entries.\n";

// Iterator used to hold position in the map

map::iterator loc;

// Returns same value as ass_array.end() if not find.

loc = ass_array.find("sugar");

if(loc != ass_array.end())
cout << "Sugar found!\n";
else
cout << "No Sugar\n";

return 0;
}

No comments: