#include <iostream>
#include <map>
using namespace std; int main()
{
方法一:
map<int,int> m;
srand((unsigned)time(NULL)); for (int i =; i< ; i++)
{
int key = rand()%+;//1~100的随机数
int value = rand()%;//0或1随机数
printf("第%d次插入前:key=%d, value=%d\n",i+,key,value);
m.insert(pair<int,int>(key,value));
}
printf("\n"); map<int,int>::iterator iter; for (iter = m.begin(); iter != m.end(); iter++)
{
printf("key:%d, value:%d\n",iter->first,iter->second);
}
printf("\n"); return ;
}
自动排序:
方法二(26个英文字母):
#include <iostream>
#include <map>
using namespace std; int main()
{
map<int,int> mc;
srand((unsigned)time(NULL)); for (char c='z'; c >= 'a'; c--)
{
int num = rand()%;
printf("%c, %d\n",c,num);
mc.insert(pair<char,int>(c,num));
} printf("\n"); map<char,int>::iterator iter; for (iter = mc.begin(); iter!=mc.end(); iter++)
{
printf("key= %c, value=%d\n",iter->first,iter->second);
} printf("\n");
return ;
}
遍历map: