C++ map的常见用法(详细)、map中insert用法、pair用法

一、 map的特性

所有元素都会根据元素的减值自动被排序。
map的所有元素都是pair,同时拥有实值(value)和键值(key)。
pair的第一个元素会被视为键值,第二个元素会被视为实值。
map不允许两个元素拥有相同的键值。
头文件:#include< map >

二、map的基本构造函数类型

 map<string , int >strMap;

 map<int ,string >intMap;

 map<sring, char>strMap;

 map< char ,string>charMap;

 map<char ,int>charMap;

 map<int ,char >intMap;

三、map的基本操作函数:

begin()     //返回指向map头部的迭代器

clear()    //删除所有元素

count()     //返回指定元素出现的次数

empty()     //如果map为空则返回true

end()      //返回指向map末尾的迭代器

equal_range()  //返回特殊条目的迭代器对

erase()     //删除一个元素

find()      //查找一个元素。查的是 健
//当所查找的关键key出现时,它返回数据所在对象的位置,如果沒有,返回iter与end()函数的值相同。

 
get_allocator() //返回map的配置器

insert()     //插入元素

key_comp()    //返回比较元素key的函数

lower_bound()  //返回键值>=给定元素的第一个位置

max_size()    //返回可以容纳的最大元素个数

rbegin()     //返回一个指向map尾部的逆向迭代器

rend()      //返回一个指向map头部的逆向迭代器

size()      //返回map中元素的个数

swap()      //交换两个map

upper_bound()   //返回键值>给定元素的第一个位置

value_comp()   //返回比较元素value的函数

四、 二、map添加数据

map<int ,string> maplive;

pair<int,string> value(1,"a");maplive.insert(value);
//等价于maplive.insert(pair<int,string>(1,"a"));

maplive.insert(map<int,string>::value_type(1,"a"));

maplive[1]="a";//map中最简单最常用的插入添加!

五、pair用法

头文件:#include< utility >

1.pair 默认对first升序,当first相同时对second升序;

类模板:template <class T1, class T2> struct pair
参数:T1是第一个值的数据类型,T2是第二个值的数据类型。

功能:pair将一对值组合成一个值,这一对值可以具有不同的数据类型(T1和T2),两个值可以分别用pair的两个公有函数first和second访问。
pair实际上可以看作一个内部有两个元素的结构体,且这两个元素的类型是可以指定的

 struct pair{
     typename1 first;
     typename2 second;
 };
2…定义(构造):
pair<int, double> p1;     //使用默认构造函数
pair<int, double> p2(1, 2.4);  //用给定值初始化
pair<int, double> p3(p2);  //拷贝构造函数
2.访问两个元素(通过first和second):
pair<int, double> p1;  //使用默认构造函数
p1.first = 1;
p1.second = 2.5;
cout << p1.first << ' ' << p1.second << endl;
3.赋值 :
//(1)利用make_pair:
pair<int, double> p1;
p1 = make_pair(1, 1.2);
//(2)变量间赋值:
pair<int, double> p1(1, 1.2);
pair<int, double> p2 = p1;
4.示例
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

#define mem(x,y) memset(x,y,sizeof(x))

using namespace std;
typedef long long LL;
const int INF=0x3f3f3f3f;

pair<int,int>pa[100];//定义

int cmp(pair<int,int>a,pair<int,int>b){
    if(a.first!=b.first)
      return a.first>b.first;
    else 
      return a.second<b.second;
}
int main(){
    int a,b;
    for(int i=0;i<5;i++){
        scanf("%d%d",&a,&b);
        pa[i]=make_pair(a,b);//赋值
    }
    
    sort(pa,pa+5,cmp);//排序
    
    for(int i=0;i<5;i++)
        printf("%d %d\n",pa[i].first,pa[i].second);
    return 0;
}

六、map示例

#include <iostream>
#include "string.h"
#include "stdio.h"
#include<map>
using namespace std;

int main(){
    map<string,int> strMap;  //以string为键值,以int为实值
    strMap[string("jjhou")] = 1;
    strMap[string("jerry")] = 2;
    strMap[string("jason")] = 3;
    strMap[string("jimmy")] = 4;

    pair<string,int> value(string("david"),5);
    strMap.insert(value);//插入新元素
    //或 strMap[健]=键值;
    
  
    //遍历
    map<string,int>::iterator iter = strMap.begin();
    for(;iter !=strMap.end();iter++)
    {
        cout<<iter->first<<' '<<iter->second<<endl;
    }
    cout<<endl;

    int number = strMap[string("jjhou")];
    cout<<number<<endl;
    cout<<endl;

  
    //查找元素
    map<string,int>::iterator iter3;
    //面对关联式容器,应该使用其所提供的find函数来搜索元素,会比使用STL算法find()更有效率。因为STL算法find()只是循环搜索。
    iter3 = strMap.find(string("mchen"));
    if(iter3 == strMap.end())
        cout<<"mchen no fount"<<endl;
        cout<<endl;

    iter3 = strMap.find(string("jerry"));
    if(iter3 != strMap.end())
        cout<<"jerry fount"<<endl;
        cout<<endl;

  
    //修改实值,键值不可修改
    iter3->second = 9; //可以通过map迭代器修改“value”(not key)
    int number1 = strMap[string("jerry")];
    cout<<number1<<endl;
    
    map<string,int>::iterator iter1 = strMap.begin();
    for(;iter1 !=strMap.end();iter1++)
    {
        cout<<iter1->first<<' '<<iter1->second<<endl;
    }
    cout<<endl;
    
  
    //删除元素
    strMap.erase(iter1);//删除一个条目
    strMap.erase(string("jason"));//根据键值删除

    map<string,int>::iterator iter2 = strMap.begin();
    for(;iter2 !=strMap.end();iter2++)
    {
        cout<<iter2->first<<' '<<iter2->second<<endl;
    }
}

七、map中insert用法

1.用insert函数插入pair数据
第一种:用insert函数插入pair数据:在VC下请加入这条语句,屏蔽4786警告 #pragma warning (disable:4786) )
map<int, string> mapStudent;
   
mapStudent.insert(pair<int, string>(1, "student_one"));
   
mapStudent.insert(pair<int, string>(2, "student_two"));
   
mapStudent.insert(pair<int, string>(3, "student_three"));
第二种:用insert函数插入value_type数据,下面举例说明
map<int, string> mapStudent;
   
mapStudent.insert(map<int, string>::value_type (1, "student_one"));
   
mapStudent.insert(map<int, string>::value_type (2, "student_two"));
   
mapStudent.insert(map<int, string>::value_type (3, "student_three"));
第三种:在insert函数中使用make_pair()函数,下面举例说明
map<int, string> mapStudent;

mapStudent.insert(make_pair(1, "student_one"));

mapStudent.insert(make_pair(2, "student_two"));

mapStudent.insert(make_pair(3, "student_three"));
第四种:用数组方式插入数据,下面举例说明
map<int, string> mapStudent;
   
mapStudent[1] = "student_one";
   
mapStudent[2] = "student_two";
   
mapStudent[3] = "student_three";
2.示例
#include <map>
#include <string>
#include <iostream>
using namespace std;
int main()
{
    map<int, string> mapStudent;
    mapStudent.insert(pair<int, string>(1, "student_one"));
    mapStudent.insert(pair<int, string>(2, "student_two"));
    mapStudent.insert(pair<int, string>(3, "student_three"));
    
    //声明了一个key为int类型,value为string类型的map,
    //用insert函数插入value_type数据,
    //插入前,需要将(1,"student_one")转换为map<int,string>::value_type数据再插入。
    
    map<int, string>::iterator iter;
    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++){
       cout<<iter->first<<" "<<iter->second<<endl;
    }
    return 0;
}
  • 22
    点赞
  • 162
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值