简体   繁体   English

具有优化查找的容器,如std :: map但非关联

[英]Container with optimized find like std::map but non associative

I have a big std::vector<int> where I have to get an iterator so that I can call other functions of it, like erase . 我有一个很大的std::vector<int> ,在这里我必须获得一个迭代器,以便可以调用它的其他函数,例如erase Looping through the vector to find the element I'm searching for takes a lot of time. 遍历向量以查找要搜索的元素会花费很多时间。

std::map::find() is much faster, but I don't want to allocate memory for the second value which I'm never going to use. std::map::find()快得多,但我不想为我永远不会使用的第二个值分配内存。

Is there any single-value container with find() or anything that gives me an iterator with similar speed as std::map::find ? 是否有任何带有find()单值容器,或者有什么可以让我获得与std::map::find类似速度的迭代器? I couldn't find any. 我找不到。

您正在寻找std::setstd::multiset

您可以使用std::unordered_map或使用相同的std::vector来保留元素的顺序,您可以将标准算法应用于已排序的容器,例如std::equal_range

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

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