ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

map unordered_map unordered_multimap

2021-04-19 11:02:51  阅读:233  来源: 互联网

标签:Node map multimap insert std a4 include unordered


 

C++ map容器插入具有相同键的键值对的覆盖问题

map容器插入键值对的方法一般有两种

  1. map["key"] = value;
  2. map.insert(make_pair<>("", ""));

 

#include <map>
#include <iostream>

using namespace std;

int main()
{
  map<double, double> mp1;
  mp1[1.0] = 1.1;
  mp1[1.0] = 1.2;
  cout << mp1.size() << endl;
  cout << mp1.begin()->second << endl;
  return 0;
}
 

输出结果为

1
1.2

 

使用方法二插入相同键的键值对时,后一组的键值对不会插入map容器,即不会覆盖前一组键值对。代码如下:

 

 

#include <map>
#include <iostream>

using namespace std;

int main()
{
  map<double, double> mp1;
  mp1.insert(make_pair<double, double>(2.0, 2.1));
  mp1.insert(make_pair<double, double>(2.0, 2.2));
  cout << mp1.size() << endl;
  cout << mp1.begin()->second << endl;
  return 0;
}
 

 

输出结果为

 

1
2.1

 

 

 

 

 

 

 

#include <string>
#include <iostream>
//查询性能最高
//允许重复的,hash_map
#include <unordered_map>
#include <algorithm>
using namespace std;


int main()
{
    //允许重复的映射
    unordered_multimap<string, double>mymap{ {"a1",113},{ "a2",143 },{ "a3",1123 } };

    mymap.insert(pair<string, double>("a4", 345));
    mymap.insert(pair<string, double>("a4", 315));
    mymap.insert(pair<string, double>("a4", 325));
    mymap.insert(pair<string, double>("a4", 335));

    /*mymap.insert(unordered_multimap<string, double>::value_type("a5", 3425));*/

    /*for (auto i : mymap)
    {
        cout << i.first << "  " << i.second << endl;
    }*/

    /*auto it = mymap.find("a1");
    if (it != mymap.end())
    {
        cout << it->second << endl;
    }*/

    //查找所有
    auto it = mymap.equal_range("a4");

    for_each(it.first, it.second, [](unordered_multimap<string, double>::value_type &x) {cout << x.first << "  " << x.second << endl; });
    cin.get();
    return 0;
}

 

 

root@ubuntu:~/c++# g++ -std=c++11  multimap.cpp -o  multimap
root@ubuntu:~/c++# ./multimap 
a4  335
a4  325
a4  315
a4  345

 

#include <string>
#include <iostream>
//查询性能最高
//允许重复的,hash_map
#include <unordered_map>
#include <algorithm>
using namespace std;


int main()
{
    //允许重复的映射
    unordered_map<string, double>mymap{ {"a1",113},{ "a2",143 },{ "a3",1123 } };

    mymap.insert(pair<string, double>("a4", 345));
    mymap.insert(pair<string, double>("a4", 315));
    mymap.insert(pair<string, double>("a4", 325));
    mymap.insert(pair<string, double>("a4", 335));

    /*mymap.insert(unordered_multimap<string, double>::value_type("a5", 3425));*/

    /*for (auto i : mymap)
    {
        cout << i.first << "  " << i.second << endl;
    }*/

    /*auto it = mymap.find("a1");
    if (it != mymap.end())
    {
        cout << it->second << endl;
    }*/

    //查找所有
    auto it = mymap.equal_range("a4");

    for_each(it.first, it.second, [](unordered_multimap<string, double>::value_type &x) {cout << x.first << "  " << x.second << endl; });
    cin.get();
    return 0;
}

 

root@ubuntu:~/c++# g++ -std=c++11  multimap.cpp -o  multimap
root@ubuntu:~/c++# ./multimap 
a4  345

root@ubuntu:~/c++# cat multimap.cpp

 

 

#include <unordered_map>
#include <string>
#include<iostream>
using namespace std;
 
 
class Node
{
public:
        Node(int age, string name);
        ~Node();
 
        bool operator==(const Node &n) const;
public:
        std::string m_strName;
        int m_iAge;
};
 
Node::Node(int age, string name) :m_strName(name),m_iAge(age)
{
}
 
Node::~Node()
{
}
 
bool Node::operator==(const Node & n) const 
{
        if (n.m_iAge==m_iAge && m_strName==n.m_strName)
        {
                return true;
        }
        return false;
}
 
struct KeyHasher
{
        std::size_t operator()(const Node& k) const
        {
                using std::size_t;
                using std::hash;
                using std::string;
                return ((hash<string>()(k.m_strName)) ^ (hash<int>()(k.m_iAge) << 1));
        }
};
 
int  main( )
{
        std::unordered_map<Node, int, KeyHasher> myMap;
        myMap.insert(pair<Node, int>(Node(24, "kobe"), 24));
        //遍历输出+迭代器的使用
        auto iter = myMap.begin();//auto自动识别为迭代器类型unordered_map<int,string>::iterator
        while (iter!= myMap.end())
         {  
                cout << iter->second << endl;  
                ++iter;  
        }  
        auto iterator = myMap.find(Node(24, "kobe"));//find()返回一个指向2的迭代器
        if (iterator != myMap.end())
            cout << iterator->first.m_strName << "  "<< iterator->second << endl; 
        myMap.insert(pair<Node, int>(Node(24, "kobe"), 40));
        iterator = myMap.find(Node(24, "kobe"));//find()返回一个指向2的迭代器
        if (iterator != myMap.end())
            cout << iterator->first.m_strName << "  "<< iterator->second << endl; 
} 
 

 

root@ubuntu:~/c++# ./unorder2 
24
kobe  24
kobe  24

 

标签:Node,map,multimap,insert,std,a4,include,unordered
来源: https://www.cnblogs.com/dream397/p/14675918.html

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

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

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

ICode9版权所有