简体   繁体   English

具有矢量用法的Unordered_map

[英]Unordered_map with vector usage

I have an unordered_map: 我有一个unordered_map:

std::tr1::unordered_map<unsigned int, vector<unsigned int> > duplicates;

Anad I would like to insert: Anad我想插入:

duplicates.insert(make_pair(1, 2));
duplicates.insert(make_pair(1, 5));
duplicates.insert(make_pair(1, 6));

into the vector because they share the same key. 向量,因为它们共享相同的密钥。 I use a for loop for inserting pairs. 我使用for循环插入对。 How can I initialise the vector if no items share the same key and it is the first time that I find that key without searching the whole unordered_map? 如果没有项目共享相同的键,并且我是第一次找到该键而不搜索整个unordered_map,那么如何初始化向量?

I do not want to first use find (to get the vector if exist) and then insert. 我不想先使用find(如果存在则获取向量)然后再插入。 Is that possible? 那可能吗?

Sorry if I misunderstood your question, but perhaps you might find use in something like unordered_multimap ? 抱歉,如果我误解了您的问题,但是也许您可能会在unordered_multimap类的地方找到用处?

Unordered multimaps are associative containers that store elements formed by the combination of a key value and a mapped value, much like unordered_map containers, but allowing different elements to have equivalent keys . 无序多图是关联容器,存储由键值和映射值的组合形成的元素,与unordered_map容器非常相似,但是允许不同的元素具有等效的键

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

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