pair 只是成组,
map 是 映射,,下标和值之间的关系
#include<iostream>
#include<string>
#include<vector>
#include<utility>
using namespace std;
int main()
{
pair<string, int>p;
typedef vector< pair<string, int> > VP;
VP vp;
while(cin>>p.first>>p.second)
{
vp.push_back(make_pair(p.first,p.second));
}
VP::iterator it;
for(it=vp.begin(); it!=vp.end(); it++)
cout<<it->first<<","<<it->second<<endl;
return 0;
}
目前map的用场是快速查找东西 像是数组 知道下标了 立刻找到 内容;
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<set>
#include<map>
using namespace std;
int main()
{
map <int, int> m1, m2, m3;
map <int, int>::iterator m1_Iter;
m1.insert ( pair <int, int> ( 1, 10 ) );
m1.insert ( pair <int, int> ( 2, 20 ) );
m1.insert ( pair <int, int> ( 3, 30 ) );
m2.insert ( pair <int, int> ( 10, 100 ) );
m2.insert ( pair <int, int> ( 20, 200 ) );
m3.insert ( pair <int, int> ( 30, 300 ) );
map<char,int>mp;
mp['m']=20;
mp['r']=30;
mp['a']=40;
for(map<char,int>::iterator it=mp.begin();it!=mp.end();it++)
{
printf("%c %d\n",it->first,it->second);
}
return 0;
}
// 定义一个map对象
map<int, string> mapStudent;
// 第一种 用insert函數插入pair
mapStudent.insert(pair<int, string>(000, "student_zero"));
// 第二种 用insert函数插入value_type数据
mapStudent.insert(map<int, string>::value_type(001, "student_one"));
// 第三种 用"array"方式插入
mapStudent[123] = "student_first";
mapStudent[456] = "student_second";