1 # include<iostream> 2 # include<map> 3 # include<cstdio> 4 using namespace std; 5 struct myComp 6 { 7 bool operator()(const int &a, const int &b) 8 { 9 if(a!=b) 10 { 11 return a>b; 12 } 13 else 14 { 15 return a>b; 16 } 17 } 18 }; 19 struct Info 20 { 21 string name; 22 float score; 23 bool operator < (const Info &a)const 24 { 25 return a.score < score; 26 } 27 }; 28 int main(int argc,const char * argv[]) 29 { 30 /*----------------------------------------*/ 31 //map创建,元素的插入和遍历 32 //map<string,float>m; 33 //m["Jack"] = 98.5; 34 //m["Bomi"] = 96.0; 35 //m["Kate"] = 97.5; 36 //map<string,float>::iterator it; 37 38 //for(it = m.begin();it!=m.end();it++) 39 //{ 40 // cout<<(*it).first<<":"<<(*it).second<<endl; 41 //} 42 43 /*---------------------------------------*/ 44 //删除元素 45 //map<int,char>m; 46 //m[25] = 'm'; 47 //m[28] = 'k'; 48 //m[10] = 'x'; 49 //m[30] = 'a'; 50 //m.erase(28); 51 52 //map<int,char>::iterator it; 53 //for(it = m.begin();it!=m.end();it++) 54 //{ 55 // cout<<(*it).first<<":"<<(*it).second<<endl; 56 //} 57 58 59 60 /*----------------------------------------*/ 61 //元素的反向逆序 62 //map<int,char>m; 63 //m[25] = 'm'; 64 //m[28] = 'k'; 65 //m[10] = 'x'; 66 //m[30] = 'a'; 67 68 //map<int,char>::reverse_iterator it; 69 //for(it = m.rbegin();it!=m.rend();it++) 70 //{ 71 // cout<<(*it).first<<":"<<(*it).second<<endl; 72 //} 73 74 75 /*----------------------------------------*/ 76 //元素的搜索 77 //map<int,char>m; 78 //m[25] = 'm'; 79 //m[28] = 'k'; 80 //m[10] = 'x'; 81 //m[30] = 'a'; 82 83 //map<int,char>::iterator it; 84 //it = m.find(28); 85 //if(it!=m.end()) 86 //{ 87 // cout<<(*it).first<<":"<<(*it).second<<endl; 88 //} 89 //else 90 //{ 91 // cout<<"not find it"<<endl; 92 //} 93 94 95 /*-------------------------------------------*/ 96 //自定义的比较函数 97 //map<int,char,myComp>m; 98 //m[25] = 'm'; 99 //m[28] = 'k'; 100 //m[10] = 'x'; 101 //m[30] = 'a'; 102 103 //map<int,char,myComp>::iterator it; 104 //for(it = m.begin();it!=m.end();it++) 105 //{ 106 // cout<<(*it).first<<":"<<(*it).second<<endl; 107 //} 108 109 110 /*------------------------------------------*/ 111 //另一种定义方法 112 //map<Info,int>m; 113 //Info info; 114 //info.name = "Jack"; 115 //info.score = 60; 116 //m[info] = 10; 117 //info.name = "Peti"; 118 //info.score = 66.5; 119 //m[info] = 30; 120 //map<Info,int>::iterator it; 121 //for(it = m.begin();it!=m.end();it++) 122 //{ 123 // cout<<(*it).second<<":"; 124 // cout<<((*it).first).name<<" "<<((*it).first).score<<endl; 125 //} 126 127 /*------------------------------------------*/ 128 //用map实现数字分离 129 //map<char,int>m; 130 //m['0'] = 0; 131 //m['1'] = 1; 132 //m['2'] = 2; 133 //m['3'] = 3; 134 // m['4'] = 4; 135 // m['5'] = 5; 136 // m['6'] = 6; 137 // m['7'] = 7; 138 // m['8'] = 8; 139 // m['9'] = 9; 140 141 /* 142 for(int j = 0;j < 10;j++) 143 { 144 m[0+'j'] = j; 145 } 146 */ 147 // string sa,sb; 148 // sa = "6234"; 149 //int i; 150 //int sum = 0; 151 //for(i = 0;i < sa.length();i++) 152 //{ 153 // sum+=m[sa[i]]; 154 // } 155 // cout<<"sum = "<<sum<<endl; 156 157 158 /*--------------------------------------------*/ 159 //数字映射字符 160 map<int ,char>m; 161 m[0] = '0'; 162 m[1] = '1'; 163 m[2] = '2'; 164 m[3] = '3'; 165 m[4] = '4'; 166 m[5] = '5'; 167 m[6] = '6'; 168 m[7] = '7'; 169 m[8] = '8'; 170 m[9] = '9'; 171 172 /* 173 for(int j = 0;j < 10;j++) 174 { 175 m[j] = '0'+j; 176 } 177 */ 178 int n = 7; 179 string s="The number is"; 180 cout<<s+m[n]<<endl; 181 182 183 184 185 186 187 }