map

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";

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值