繁体   English   中英

在 c++ 中插入无序 map

[英]insertion in unordered map in c++

#include<iostream>
#include<unordered_map>
#include<list>
#include<cstring>

using namespace std;
class Graph {
    unordered_map<string, list<pair<string, int>>>l;
public:
    void addedge(string x, string y, bool bidir, int wt) {
        l[x].push_back(make_pair(y, wt));
        if (bidir) {
            l[y].push_back(make_pair(x, wt));
        }
    }
};

int main()
{
    Graph g;
    g.addedge("A", "B", true, 20);
    return 0;
}

unordered map in c++ dont uses push_back() function for insertion then how this unordered map 'l' is using push_back() function.

当您在l上使用下标运算符时,例如在l[x]中,它返回对映射到x的引用(或插入默认构造值并返回对它的引用)。

在这种情况下,值的类型是std::list<std::pair<std::string, int>>并且类型具有push_back成员 function。

与此相同:

std::list<std::pair<std::string, int>>& listref = l[x];
listref.push_back(make_pair(y, wt));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM