STL Maps---c++

the syntax by using Maps  is :

#include <map>

using namespace std;

std::map <key_type, data_type, [comparison_function]>
ex:
map <string,char>grade_list;
grade_list["lzm"] = 'A';  
grade_list.erase("lzm");
grade_list.size();
grade_list.clear();
//the iterator can be used to map syntax:
std::map<parameters>::iterator iterator_name;

grade_list.begin()->first

 

code:

#include<iostream>

#include<map>

using namespace std;

int main(){
   map<string,char>mygrade;
   //using iterator
   map<string,char>::iterator grade;
   
   mygrade["math"] = 'A';
   mygrade["chinese"] = 'B';
   mygrade["english"] = 'B';
   mygrade["technology"] = 'A';
   cout << mygrade["chinese"] << endl;  
    // output B 
   cout << mygrade.begin()->first<<endl;
   //output chinese
   cout << mygrade.begin()->second<<endl;
     // output B 
  // cout << mygrade.end()->first<<endl;  
   //Segmentation fault (core dumped)
  // cout << mygrade.end()->second<<endl;  
   //output nothing
   //cout << mygrade.begin()<<endl;  
//  grade = mygrade.begin()->first;
  // cout << *grade<<endl;  
  for(grade=mygrade.begin();grade != mygrade.end();grade++){
        cout << (*grade).first<<":"<<(*grade).second<<";"<<endl;
  }
  /*
  output:
  B
  chinese
  B
  chinese:B;
  english:B;
  math:A;
  technology:A;

  
  */
   return 0; 
}

i spent much time to sovling some problem  about iterator .

but at last ,we can pass code ,and output the answer by using combination map and iterator.

posted on 2014-10-22 23:24  lzm420241  阅读(114)  评论(0)    收藏  举报

导航