如何使用映射 C++ 中的值获取匹配的键
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12742472/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to get matching key using the value in a map C++
提问by snazziii
I have a map with a struct as a value type
我有一个带有结构作为值类型的地图
map<int id, struct_t*> table
struct_t
{
int prev;
int wt;
string name;
}
Using only prev, I need to find the corresponding id. Thanks so much in advance!
仅使用prev,我需要找到相应的id。非常感谢!
EDIT:
编辑:
int key=0;
for(auto it = table.begin(); it != table.end(); ++it)
{
if(table[(*it).first].prev == ?)
}
This is how my map data looks like:
这是我的地图数据的样子:
id prev abundance thing
1573 -1 0 book
1864 1573 39 beds
2075 1864 41 tray
1760 2075 46 cups
For each id, I need to find the NEXT matching id. So, for 1573 from the prev column I need to find a matching 'id' which is 1864. Also, std::next doesn't work because the data set can have the matching ids not necessarily in the next element.Hope this helps!
对于每个 id,我需要找到 NEXT 匹配的 id。因此,对于 prev 列中的 1573,我需要找到一个匹配的“id”,即 1864。此外,std::next 不起作用,因为数据集可以在下一个元素中具有匹配的 id,但不一定在下一个元素中。希望这有帮助!
PLEASE PLEASE help me!!! MY boss is already disappointed that I'm taking so much time to learn C++ (its been 3 weeks already!)
请帮助我!!!我的老板已经对我花这么多时间学习 C++ 感到失望(已经 3 周了!)
采纳答案by Mark Ingram
If you've got a modern compiler (supports lambdas), you can do the following:
如果您有现代编译器(支持 lambdas),您可以执行以下操作:
const int prevToFind = 10;
auto findResult = std::find_if(std::begin(table), std::end(table), [&](const std::pair<int, struct_t*> &pair)
{
return pair.second->prev == prevToFind;
});
int foundKey = 0; // You might want to initialise this to a value you know is invalid in your map
struct_t *foundValue = nullptr
if (findResult != std::end(table))
{
foundKey = findResult->first;
foundValue = findResult->second;
// Now do something with the key or value!
}
Let me know if you have an older compiler, and I can update the example to use a predicate class instead.
如果您有一个较旧的编译器,请告诉我,我可以更新示例以使用谓词类。
回答by fasked
Simple loop can do it:
简单的循环可以做到:
#include <map>
#include <string>
#include <iostream>
int main()
{
std::map<int, std::string> m = {
std::make_pair(0, "zero"), std::make_pair(1, "one"), std::make_pair(2, "two")
};
int key = 0;
for (auto &i : m) {
if (i.second == "two") {
key = i.first;
break; // to stop searching
}
}
std::cout << key << std::endl;
}
Of course you need to set up your own if-statement for searching. Please note, boost bidirectional map could be a solution (boost::bimap)
当然,您需要设置自己的 if 语句进行搜索。请注意,boost 双向映射可能是一个解决方案(boost::bimap)
回答by Hendrik
Looping over the map of course does the trick, but you may want to consider using a second map as an index:
循环地图当然可以解决问题,但您可能需要考虑使用第二张地图作为索引:
map<int,int> table_idx;
Whenever you add new entries to table
you will need to update table_idx
as well, storing the id
that corresponds to every prev
. table_idx
will then allow you to reverse-lookup the id
in log(N) time:
每当您添加新条目时,table
您也需要更新table_idx
,存储id
对应于每个prev
. table_idx
然后将允许您反向查找id
log(N) 时间:
int prev_for_id = table_idx[id];
回答by NoSenseEtAl
Im getting a feeling that you are a beginner so it would be nice if you would tell us what are you trying to do because maybe you are trying to solve a wrong problem.
Like noted maps are designed to be searched by the key, not value.
That being said if you insist on searching the map this way you will problably wanna check out Boost Bimap.
我有一种感觉,你是一个初学者,所以如果你能告诉我们你想做什么,那会很好,因为也许你正试图解决一个错误的问题。
类似的映射被设计为通过键而不是值进行搜索。
话虽如此,如果您坚持以这种方式搜索地图,您可能会想要查看Boost Bimap。