从std :: map值获取密钥的有效方法

rim*_*rim 6 c++ performance stdmap

我有一张地图如下:

std::map< std::string ,int> mapobj;
mapobj["one"] = 1;
mapobj["two"] = 2;
mapobj["three"] =3 ;
Run Code Online (Sandbox Code Playgroud)

输入值时如何获取键

EX:

输入:1

输出:一

注意:在我的情况下,值是唯一的

pax*_*blo 9

一对一映射实际上非常简单,最快的方法是维护两个映射,每个映射一个.如果它不是一对一的话会变得更加复杂,因为你需要提供一种方法来获取一值或键,而不是一个.令人高兴的是,你只有一对一的要求.

其中一个地图是你现在拥有的地图,另一个地图将值映射到给定的密钥,soboth将是:

std::map<std::string, int> forwardmapobj;
std::map<int, std::string> reversemapobj;
Run Code Online (Sandbox Code Playgroud)

这些将保持在某种bidimap类中.

无论何时插入或删除bidimap,都必须在两个内部映射上执行等效操作.

例如,这是一些伪代码.它维护着两个映射,并确保它们与您更改键和值的任何操作保持同步:

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]
Run Code Online (Sandbox Code Playgroud)

显然,你可以添加许多其他东西,但这应该构成基础.无论如何,在你把它变成C++类之前,你可能已经有了足够的工作:-)


如果你希望推出自己的解决方案,然后升压有一个非常好的一个,你可以作为是很好用.Boost.Bimap提供了一个完全模板化的双向映射,您应该能够使用最少的代码,例如以下完整的程序:

#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;
}
Run Code Online (Sandbox Code Playgroud)

它在数字的文本形式和积分值之间创建双向映射,然后进行一些查找(在两个方向上)以显示它的工作原理:

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
Run Code Online (Sandbox Code Playgroud)


dgn*_*uff 6

我注意到它有"stdmap"标签,所以这可能不合适.然而升压boost::bimap<>这将让你做你想做的:它允许通过查找任何键或值.