admin管理员组文章数量:1123153
I'm writing some C code using GLib and using GHashTable to store some data. However, sometimes I need to do a "reverse lookup" where I need to find the first key that matches a given value. I tried looking through the documentation for GHashTable, but I couldn't find anything like this. Initially, I was hopeful that g_hash_table_find ()
could do this, but it only returns the value of the key/value pair, so it can't do what I'm asking for.
How can I get the first key that matches a given value in a GHashTable? The definition of "first" doesn't really matter as all values should be unique in my case.
I'm writing some C code using GLib and using GHashTable to store some data. However, sometimes I need to do a "reverse lookup" where I need to find the first key that matches a given value. I tried looking through the documentation for GHashTable, but I couldn't find anything like this. Initially, I was hopeful that g_hash_table_find ()
could do this, but it only returns the value of the key/value pair, so it can't do what I'm asking for.
How can I get the first key that matches a given value in a GHashTable? The definition of "first" doesn't really matter as all values should be unique in my case.
Share Improve this question asked 7 hours ago NewbyteNewbyte 3,1706 gold badges26 silver badges51 bronze badges 2 |1 Answer
Reset to default 0How can I get the first key that matches a given value in a GHashTable in C?
Hash tables are inherently unordered, so "first key" doesn't make much sense in that context. What you can do is to get the list of keys as an array, sort the array, and then iterate over it checking the corresponding value until you find the one that you're looking for.
本文标签: hashtableHow can I get the first key that matches a given value in a GHashTable in CStack Overflow
版权声明:本文标题:hashtable - How can I get the first key that matches a given value in a GHashTable in C? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736553045a1944539.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
GHashTableIter
iterator that you break out of upon finding a matching value. – Shawn Commented 6 hours ago