ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

C++11mutimap 与map用法总结

2021-01-09 12:04:00  阅读:291  来源: 互联网

标签:map mapStudent insert 11mutimap C++ iter include cout


一、map 用法

1、map数据的插入

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace::std;
//数据插入
    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"));
    mapStudent.insert(map<int, string>::value_type(4, "student_four"));
    mapStudent.insert(pair<int, string>(6, "student_six"));
    mapStudent.insert(make_pair(7, "student_seven"));
    mapStudent[5] = "student_five";

 2、map数据的遍历

 //数据遍历
    //正向遍历
    map<int, string>::iterator  iter;
    for (iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    {
        cout << iter->first << " " << iter->second << endl;
    }
    //反向遍历
    map<int, string>::reverse_iterator it;
    for (it = mapStudent.rbegin(); it != mapStudent.rend(); it++)
    {
        cout << it->first << " " << it->second << endl;
    }
    //数组形式的遍历
    int nSize = mapStudent.size();
    for (int i = 0; i < nSize; i++)
    {
        cout << mapStudent[i] << endl;
    }

3、数据的查找

map<int, string>::iterator iter1;

    iter1 = mapStudent.find(1);

    if (iter1 != mapStudent.end())
    {
        cout << iter1->second << endl;
    }
    else
    {
        cout << "can't find it " << endl;
    }
   //判断是否存在这个key
    int judge = mapStudent.count(1);

    if (judge)
    {
        cout << "exist" << endl;
    }
    else
    {
        cout << "not exist" << endl;
    }

4、数据的清空与删除 

//数据的清空与判空
   mapStudent.clear();
    if (!mapStudent.empty())
        cout << "not empty" << endl;
    else
        cout << "empty" << endl;

    //数据的删除 
    map<int, string> ::iterator it3;
    it3 = mapStudent.find(1);
    mapStudent.erase(it3);/*删除指定的键值*/

 

judge = mapStudent.erase(2);
    if (judge)
    {
        cout << "delete successfully" << endl;
        for (it3 = mapStudent.begin(); it3 != mapStudent.end(); it3++)
        {
            cout << it3->second << endl;
        }
    }

二、mutimap用法

MultiMap允许重复的元素。

C++ stl  Multimap的基本操作成员函数列表介绍如下:

begin()返回指向第一个元素的迭代器

clear()删除所有元素

count()返回一个元素出现的次数

empty()如果multimap为空则返回真

end()返回一个指向multimap末尾的迭代器

equal_range()返回指向元素的key为指定值的迭代器对

erase()删除元素

find()查找元素

get_allocator()返回multimap的配置

insert()插入元素

key_comp()返回比较key的函数

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

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

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

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

size()返回multimap中元素的个数

swap()交换两个multimaps

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

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

1、mutimap 数据插入

构造mutimap ,遍历输出

#include <iostream>
#include <map>
#include <string>
#include <vector>
#include <algorithm>
using namespace::std;
int main()
{
    multimap<string, string> m = {
           {"India", "New Delhi"}, {"India", "Hyderabad"}, {"United Kingdom", "London"}, {"United States", "Washington D.C"}
    };
    m.insert(pair<string,string>("China","Beijing"));
    m.insert(make_pair("China", "Shanghai"));

    cout << "Size of map m: " << m.size() << endl;
    cout << "Elements in m: " << endl;

    for (multimap<string, string>::iterator it = m.begin(); it != m.end(); ++it)
    {
        cout << "  [" << (*it).first << ", " << (*it).second << "]" << endl;
    }

    
}

 

2、遍历单键查询与范围查询 

 /*单键查询*/
    int count = m.count("India");
    multimap<string, string>::iterator iter = m.find("India");
    for (int i = 0; i < count; i++, iter++) {
        cout << iter->first << " " << iter->second << endl;
    }

 3、mutimap删除指定键

    iter = m.find("India");
    m.erase(iter);

4、mutimap 全部删除 

    if (!m.empty()){
        m.clear();
     }

 

参考文献:

https://www.cnblogs.com/liuxiaoqing1/p/14006858.html

https://www.cnblogs.com/pprp/p/7395890.html

标签:map,mapStudent,insert,11mutimap,C++,iter,include,cout
来源: https://blog.csdn.net/kenjianqi1647/article/details/112298638

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有