map容器

13 篇文章 0 订阅
11 篇文章 0 订阅

map容器

map容器基本概念

简介:

  • map中所有元素都是pair

  • pair中第一个元素为key(键值),起到索引作用,第二个元素为value(实值)

  • 所有元素都会根据元素的键值自动排序(键值小到大排)

本质:

  • map/multimap属于关联式容器,底层结构是用二叉树实现

优点:

  • 可以根据key值快速找到value值

map和multimap区别:

  • map不允许容器中有重复key值元素

  • multimap允许容器中有重复key值元素

  • 对组pair<键值类型,实值类型>(a,b); //a b 为对应类型的数

map容器的使用(表格)

函数意义使用
size()返回容器中元素的数目cout << "map容器大小为" << a.size() << endl;
empty()判断容器是否为空if(a.empty()) { cout << "容器为空" << endl; } else { cout << "容器不为空" << endl; cout << "map容器大小为" << a.size() << endl; }
swap(st)交换两个集合容器a.swap(b);
a.insert(pair<int, int>(b, c));(括号里为对组)在容器中插入元素a.insert(pair<int, int>(b, c));
a.insert(make_pair(b,c));在容器中插入元素
a.insert(map<int, int>::value_type(b, c));在容器中插入元素
a[i] = c;在容器中插入元素[i]里面的i可以为非常量,最好别用里面输入的东西不太对劲
clear()清除所有元素a.clear();
erase(pos)删除pos迭代器所指的元素,返回下一个元素的迭代器a.erase(a.begin());//删除一个
erase(beg,end)删除区间beg,end的所有元素,返回下一个元素的迭代器a.erase(a.begin(), a.end());
erase(key)删除容器中值为key的元素a.erase(2);//2为key 无则没删东西
find(key)查找key是否存在,若存在,返回该键的元素的迭代器,若不存在,返回set.end()cout <<"键值"<<m<<"的实值为" << a.find(m)->second << endl;
count(key)统计key的元素个数map容器只能是 0或1 cout <<"统计元素的键值个数为" << a.count(x) << endl;

map构造和赋值

功能描述:

  • 对map容器进行构造和赋值操作

函数原型:

构造

 

 

#include<iostream>
#include<map>
#include<string>
using namespace std;
​
void insert(map<int,int>&a)
{
    int d;
    cout << "输入要存入的数据量" << endl;
    cin >> d;
    
    
    for (int i = 0; i < d; i++) 
    {
        int b;
        int c;
        cout << "输入键值" << endl;
        cin >> b;
        cout << "输入实值" << endl;
        cin >> c;
        a.insert(pair<int, int>(b, c));
    }
}
​
void printmap(map<int,int>&a) 
{
    for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << "键值为" << it->first<<"实值为"<<(*it).second << endl;
    }
}
int main() 
{
    map<int, int>a;
    insert(a);
    printmap(a);
    cout << endl;
    cout << endl;
    map<int, int>b(a);
    printmap(b);
    cout << endl;
    cout << endl;
    map<int, int>c;
    c = a;
    printmap(c);
​
    system("pause");
    return 0;
}

map容器大小和交换

功能描述:

  • 统计map容器大小以及交换map容器

 

#include<iostream>
#include<map>
#include<string>
using namespace std;
​
void insert(map<int,int>&a)
{
    int d;
    cout << "输入要存入的数据量" << endl;
    cin >> d;
    
    
    for (int i = 0; i < d; i++) 
    {
        int b;
        int c;
        cout << "输入键值" << endl;
        cin >> b;
        cout << "输入实值" << endl;
        cin >> c;
        a.insert(pair<int, int>(b, c));
    }
}
​
void printmap(map<int,int>&a) 
{
    for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << "键值为" << it->first<<"实值为"<<(*it).second << endl;
    }
}
int main() 
{
    map<int, int>a;
    insert(a);
    printmap(a);
    cout << endl;
    cout << endl;
​
    if(a.empty())
    {
        cout << "容器为空" << endl;
    }
    else
    {
        cout << "容器不为空" << endl;
        cout << "map容器大小为" << a.size() << endl;
    }
​
    map<int, int>b;
    insert(b);
    printmap(b);
​
    cout<< endl;
    cout << endl;
    cout << "map容器交换后" << endl;
    a.swap(b);
    printmap(a);
    printmap(b);
​
    system("pause");
    return 0;
}

map容器插入和删除

功能描述:

  • map容器进行插入数据和删除数据

  • 不推荐使用a[i] = c; [i]里面的i可以为非常量 ,[]不建议插入,用途可以利用key访问value(如果使用其查看不存在会自动创建出不存在的)

 

#include<iostream>
#include<map>   
#include<string>
using namespace std;
​
void insert(map<int,int>&a)
{
    int d;
    cout << "输入要存入的数据量" << endl;
    cin >> d;
    
    
    for (int i = 0; i < d; i++) 
    {
        int b;
        int c;
        cout << "输入键值" << endl;
        cin >> b;
        cout << "输入实值" << endl;
        cin >> c;
​
        //a.insert(pair<int, int>(b, c));
​
        a.insert(make_pair(b,c));
    
        a.insert(map<int, int>::value_type(b, c));
​
        a[i] = c;
    }
}
​
void printmap(map<int,int>&a) 
{
    for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << "键值为" << it->first<<"实值为"<<(*it).second << endl;
    }
}
int main() 
{
    map<int, int>a;
    insert(a);
    printmap(a);
    cout << endl;
    cout << endl;
​
    if(a.empty())
    {
        cout << "容器为空" << endl;
    }
    else
    {
        cout << "容器不为空" << endl;
        cout << "map容器大小为" << a.size() << endl;
    }
​
    map<int, int>b;
    insert(b);
    printmap(b);
​
    cout<< endl;
    cout << endl;
    cout << "map容器交换后" << endl;
    a.swap(b);
    printmap(a);
    printmap(b);
​
    a.erase(a.begin());//删除一个
    a.erase(a.begin(), a.end());
    a.clear();//与上面一样
    a.erase(2);//2为key  无则没删东西
​
    system("pause");
    return 0;
}

map查找和统计

功能描述:

  • 对map容器进行查找数据以及统计数据

#include<iostream>
#include<map>   
#include<string>
using namespace std;
​
void insert(map<int,int>&a)
{
    int d;
    cout << "输入要存入的数据量" << endl;
    cin >> d;
    
    
    for (int i = 0; i < d; i++) 
    {
        int b;
        int c;
        cout << "输入键值" << endl;
        cin >> b;
        cout << "输入实值" << endl;
        cin >> c;
​
        //a.insert(pair<int, int>(b, c));
​
        a.insert(make_pair(b,c));
    
        //a.insert(map<int, int>::value_type(b, c));
​
        //a[i] = c;
    }
}
​
void printmap(map<int,int>&a) 
{
    for (map<int, int>::iterator it = a.begin(); it != a.end(); it++) 
    {
        cout << "键值为" << it->first<<"实值为"<<(*it).second << endl;
    }
}
int main() 
{
    map<int, int>a;
    insert(a);
    printmap(a);
    cout << endl;
    cout << endl;
    int m;
    cout << "输入要查找键值" << endl;
    cin >> m;
​
    map<int,int>::iterator pos=a.find(m);
    if (pos != a.end())
    {
        cout << "查到元素" << endl;
        cout << "键值" << m << "的实值为" << a.find(m)->second << endl;
    }
    else 
    {
        cout << "未找到元素" << endl;
    }
    int x;
    cout << "输入要统计元素个数的键值" << endl;
    cin >> x;
    a.count(x);
    cout <<"统计元素的键值个数为" << a.count(x) << endl;
​
    system("pause");
    return 0;
}

map容器排序

  • map容器默认排序规则为 按照key值进行 ,从小到大排序,掌握如何改变排序规则

  • 主要技术点:利用仿函数,可以改变排序规则

  • bool operator()(int a,int b)const
    
    {
    
    return a>b;
    
    }
    
    for (map<int, int, mycompare>::const_iterator it = a.begin(); it != a.end(); it++)

#include<iostream>
#include<map>   
#include<string>
using namespace std;
​
class  mycompare
{
public:
    bool operator()(int a, int b)const
    {
        return a > b;
    }
};
​
​
int main() 
{
    map<int, int,mycompare>a;
    /*int d;
    cout << "输入要存入的数据量" << endl;
    cin >> d;
​
​
    for (int i = 0; i < d; i++)
    {
        int b;
        int c;
        cout << "输入键值" << endl;
        cin >> b;
        cout << "输入实值" << endl;
        cin >> c;
​
        //a.insert(pair<int, int>(b, c));
​
        a.insert(make_pair(b, c));
​
        //a.insert(map<int, int>::value_type(b, c));
​
        //a[i] = c;
    }
    */
    a.insert(make_pair(1, 2));
    a.insert(make_pair(5, 8));
    a.insert(make_pair(2, 3));
    a.insert(make_pair(3, 7));
    for (map<int, int, mycompare>::const_iterator it = a.begin(); it != a.end(); it++)
    {
        cout << "键值为" << it->first << "实值为" << (*it).second << endl;
    }
    
    
​
    
​
    system("pause");
    return 0;
}

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一只符华单推人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值