第一种:用insert函数插入pair数据,下面举例说明:html
#include <iostream> #include <map> using namespace std; int main() { map<int,string> mp; mp.insert(pair<int,string>(1,"Tom")); mp.insert(pair<int,string>(2,"Alice")); mp.insert(pair<int,string>(3,"Bob")); map<int,string>::iterator it; for(it=mp.begin();it!=mp.end();it++){ cout<<it->first<<" "<<it->second<<endl; } return 0; }第二种:用数组方式插入数据,下面举例说明:
#include <iostream> #include <map> using namespace std; int main() { map<int,string> mp; mp[1]="Tom"; mp[2]="Alice"; mp[3]="Bob"; map<int,string>::iterator it; for(it=mp.begin();it!=mp.end();it++){ cout<<it->first<<" "<<it->second<<endl; } return 0; }
#include <iostream> #include <map> using namespace std; int main() { map<int,string> mp; mp.insert(map<int, string>::value_type (1, "Tom")); mp.insert(map<int, string>::value_type (2, "Alice")); mp.insert(map<int, string>::value_type (3, "Bob")); mp.insert(map<int,string>::value_type(4,"Kim")); map<int,string>::iterator it; for(it=mp.begin();it!=mp.end();it++){ cout<<it->first<<" "<<it->second<<endl; } return 0; }
#include <iostream> #include <map> using namespace std; int main() { map<int,string> mp; mp.insert(pair<int,string>(3,"Bob")); mp.insert(pair<int,string>(1,"Tom")); mp.insert(pair<int,string>(4,"John")); mp.insert(pair<int,string>(2,"Alice")); mp.insert(pair<int,string>(5,"Cody")); //前向迭代器 map<int,string>::iterator it; for(it=mp.begin();it!=mp.end();it++){ cout<<it->first<<" "<<it->second<<endl; } //反相迭代器 map<int,string>::reverse_iterator rit; for(rit=mp.rbegin();rit!=mp.rend();rit++){ cout<<rit->first<<" "<<rit->second<<endl; } //数组遍历 int nSize = mp.size(); for(int nindex=1;nindex<=nSize;nindex++){ //此处应注意,应该是 for(int nindex = 1; nindex <= nSize; nindex++) //而不是 for(int nindex = 0; nindex < nSize; nindex++) cout<<mp[nindex]<<endl; } return 0; }
#include <iostream> #include <map> using namespace std; int main() { //cout << "Hello, World!" << std::endl; map<int,string> mp; mp.insert(pair<int,string>(1,"Tom")); mp.insert(pair<int,string>(2,"Alice")); mp.insert(pair<int,string>(3,"Bob")); map<int,string>::iterator it; it = mp.find(3); if(it != mp.end()) cout<<"Find, the value is "<<it->second<<endl; else cout<<"Do not Find"<<endl; return 0; }
#include <iostream> #include <map> using namespace std; int main() { map<int,string> mp; mp.insert(pair<int,string>(1,"Tom")); mp.insert(pair<int,string>(2,"Alice")); mp.insert(pair<int,string>(3,"Bob")); mp.insert(pair<int,string>(4,"John")); mp.insert(pair<int,string>(5,"Cody")); map<int,string>::iterator it; it = mp.lower_bound(1);//返回的是下界为1的迭代器 cout<<it->second<<endl; it = mp.lower_bound(2);//返回的是下界为2的迭代器 cout<<it->second<<endl; it = mp.lower_bound(3);//返回的是下界为3的迭代器 cout<<it->second<<endl; it = mp.upper_bound(2);//返回的是上界为3的迭代器 cout<<it->second<<endl; it = mp.upper_bound(3);//返回的是上界为4的迭代器 cout<<it->second<<endl; pair<map<int, string>::iterator, map<int, string>::iterator> mappair; mappair = mp.equal_range(2); if(mappair.first == mappair.second) cout<<"Do not Find"<<endl; else cout<<"Find"<<endl; mappair = mp.equal_range(3); if(mappair.first == mappair.second) cout<<"Do not Find"<<endl; else cout<<"Find"<<endl; return 0; }