从std :: map值获取密钥的有效方法 [英] efficient way to get key from std::map value
问题描述
我的地图如下:
std::map< std::string ,int> mapobj;
mapobj["one"] = 1;
mapobj["two"] = 2;
mapobj["three"] =3 ;
输入为值时如何获取键
EX:
输入:1
输出:一个
注意:在我的情况下,值是唯一的
推荐答案
一对一映射实际上非常容易,最快的方法是维护两个 映射,一个每个方向.如果不是一对一的,它将变得更加复杂,因为您需要提供一种获取值或键的 collection 的方法,而不是单个方法.幸运的是,您只有一对一的要求.
A one-to-one mapping is actually quite easy, the fastest way to do it is to probably maintain two maps, one for each direction. It becomes more complicated if it's not one-to-one since you'll need to provide a way to get a collection of values or key, rather than a single one. Happily, you only have the one-to-one requirement.
其中一张地图就是您现在拥有的一张,另一张地图会将值映射到给定的键,两者都是:
One of the maps is the one you have now, the other will map the values to a given key, soboth would be:
std::map<std::string, int> forwardmapobj;
std::map<int, std::string> reversemapobj;
,这些将在某种 bidimap
类中维护.
and these would be maintained within a bidimap
class of some sort.
每当您插入或删除 bidimap
时,都必须在两个内部地图上执行等效的操作.
Whenever you insert to, or delete from, your bidimap
, you have to perform the equivalent operation on both internal maps.
例如,这是一些伪代码.它会维护两个映射,并确保它们与您执行的更改键和值的任何操作保持同步:
For example, here's some pseudo-code. It maintains the two maps and ensures that they'e kept in sync for whatever operations you have that change the keys and values:
class biDiMap:
map<string, int> forwardMap
map<int, string> reverseMap
void add(string key, int val):
if exists forwardMap[key]: throw exception 'duplicate key'
if exists reverseMap[val]: throw exception 'duplicate value'
forwardMapObj[key] = val
reverseMapObj[val] = key
void delKey(string key):
if not exists forwardMap[key]: throw exception 'no such key'
delete reverseMap[forwardMap[key]]
delete forwardMap[key]
void delVal(int val):
if not exists reverseMap[val]: throw exception 'no such value'
delete forwardMap[reverseMap[val]]
delete reverseMap[val]
int getValFor(string key): return forwardMap[key]
string getKeyFor(int val): return reverseMap[val]
很显然,您可以添加很多 other 东西,但这应该构成基础.无论如何,在将其转换为C ++类之前,您可能已经做了足够的 工作:-)
Obviously, there's plenty of other stuff you could add but that should form the basis. In any case, you've probably got enough work ahead of you turning that into a C++ class :-)
如果您不想想要推出自己的解决方案,那么Boost会提供一个很好的解决方案,您可以按原样使用它. Boost.Bimap
提供了一个完全模板化的双向地图,您应该可以使用最少的代码来使用它,例如以下完整程序:
If you don't want to roll your own solution, then Boost has a very good one that you can pretty well use as is. Boost.Bimap
provides a fully-templated bi-directional map that you should be able to use with minimal code, such as the following complete program:
#include <iostream>
#include <string>
#include <boost/bimap.hpp>
using std::string;
using std::cout;
using std::exception;
using boost::bimap;
int main()
{
typedef bimap<string, int> SiMap;
typedef SiMap::value_type SiEntry;
SiMap bidi;
bidi.insert(SiEntry("ninety-nine", 99));
int i = 0;
for (string str: {"one", "two" , "three", "four", "five", "six"}) {
bidi.insert(SiEntry(str, ++i));
}
cout << "The number of entries is " << bidi.size() << "\n\n";
for (auto i = 1; i <= 7; i += 3) {
try {
cout << "Text for number " << i << " is " << bidi.right.at(i) << "\n";
} catch (exception &e) {
cout << "Got exception looking up number " << i << ": " << e.what() << "\n";
}
}
cout << "\n";
for (auto str: {"five", "ninety-nine", "zero"}) {
try {
cout << "Number for text '" << str << "' is " << bidi.left.at(str) << "\n";
} catch (exception &e) {
cout << "Got exception looking up text '" << str << "': " << e.what() << "\n";
}
}
cout << "\n";
return 0;
}
它在数字的文本形式和整数值之间创建双向映射,然后进行一些查找(双向)以表明其有效:
It creates a bi-directional mapping between the textual form of a number and the integral value, then does a few lookups (in both directions) to show that it works:
The number of entries is 7
Text for number 1 is one
Text for number 4 is four
Got exception looking up number 7: bimap<>: invalid key
Number for text 'five' is 5
Number for text 'ninety-nine' is 99
Got exception looking up text 'zero': bimap<>: invalid key
这篇关于从std :: map值获取密钥的有效方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!