for loop cpp map

map<char,int> first;
    first['a']=1;
    first['b']=2;
    first['c']=3;
    first['d']=4;
    first['e']=5;

    first['a']=11;      //overrite 1
    map<char,int>::iterator it;
    for(it=first.begin();it!=first.end();++it){
        cout<<it->first<<"="<<it->second<<endl;
    }

    typedef map<string,float> SFMap;
    SFMap second;
    second["abcdefg"]=3.1215926;
    second["sdfsdf"]=234234;
    second["sdfsdfdfdf"]=3219462;
    for(SFMap::iterator it=second.begin();it!=second.end();++it){
        cout<<it->first<<"="<<it->second<<endl;
    }

/*****************************************/
// constructing maps
#include <iostream>
#include <map>
using namespace std;

bool fncomp (char lhs, char rhs) {return lhs<rhs;}

struct classcomp {
  bool operator() (const char& lhs, const char& rhs) const
  {return lhs<rhs;}
};

int main ()
{
  map<char,int> first;

  first['a']=10;
  first['b']=30;
  first['c']=50;
  first['d']=70;

  map<char,int> second (first.begin(),first.end());

  map<char,int> third (second);

  map<char,int,classcomp> fourth;                 // class as Compare

  bool(*fn_pt)(char,char) = fncomp;
  map<char,int,bool(*)(char,char)> fifth (fn_pt); // function pointer as Compare

  return 0;
}

  

posted @ 2011-10-02 21:22  庚武  Views(271)  Comments(0)    收藏  举报