Monday, April 09, 2007

C++: Associative Arrays

#include <map>
#include <string>


using namespace std;

int main()
{
std::map<std::string, std::string> m;
m["ENG"] = "English";
m["FRA"] = "France";
m["CAN"] = "Canada";
m["AUS"] = "Australia";

//does print empty string
cout<<m["Germany"]<<endl;

//iterate through all elements of array
std::map<string, string>::iterator curr,end;
for( curr = m.begin(), end = m.end(); curr != end; curr++ )
cout << curr->first + " = " + curr->second << endl;
return 0;
}


C++ maps or associative arrays are implemented as self-balancing binary search tree. This means that when you iterate through your map it will not be in the order you added them like in PHP but instead will be in sorted order.

source(s):
http://www.webmasterworld.com/html/3249762.htm

No comments: