home   articles   tags   browse code   

C++ Associative Array


 

Quick example program with a C++ map, including how to iterate through the sample data. We should refer to the data in the map as keys and values (keys are unique indexes into their associated value). I have noticed that when I try to access a key that is not in the map, it returns an empty string.

#include <map>
#include <string>
#include <iostream>

using namespace std;

int main(int argc, char **argv)
{
   map<string, string> m;
   m["Harold Spencer"] = "515 Northland Dr NW";
   m["Jackson Porter"] = "7003 Parkway Avenue SE";
   m["Janice Heinz"] = "441 East 400 North";

   map<string, string>::iterator curr,end;
   for( curr = m.begin(), end = m.end();  curr != end;  curr++ )
       cout <<  curr->first + " : " + curr->second << endl;
   return 0;
}
 

Tags: c++
 
maarlin on Jan 5th, 2011 2:55 am said:
Instead of "+" operators should be used
 

maarlin on Jan 5th, 2011 2:57 am said:
It has removed the last important chars of my comment, so again: < < (http://www.fileformat.info/info/unicode/char/003c/index.htm )
 



 

 



Related Articles
 


home  |  privacy policy  |  terms of use  |  contact  


©2012, Zedwood.com

zedwood.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com