具有多值的无序映射及其查找

Unordered Map with Multiple Value and it's lookup

我正在做一项作业,我得到了一个很大的制表符分隔值 txt 文件,我必须使用良好的数据结构来搜索它。现在我面临的问题是,我不知道我是否在这个 unordered_map 中正确插入了值,键是一个字符串,值是一个结构向量。 其次,如果我想搜索它会怎么做?例如,如果我输入第一个 ID,如何打印或访问结构中存在的其他元素?

下面是示例数据和代码

id    effectiveTime    active    moduleId    definitionStatusId
100005    20020131    0    900000000000207008    900000000000074008
101009    20020131    1    900000000000207008    900000000000074008
102002    20020131    1    900000000000207008    900000000000074008
103007    20020131    1    900000000000207008    900000000000074008
104001    20020131    1    900000000000207008    900000000000073002
105000    20040731    0    900000000000207008    900000000000074008
106004    20020131    1    900000000000207008    900000000000074008
107008    20020131    1    900000000000207008    900000000000074008
108003    20020131    1    900000000000207008    900000000000074008
109006    20020131    1    900000000000207008    900000000000074008
110001    20020131    1    900000000000207008    900000000000074008
111002    20020131    1    900000000000207008    900000000000074008
112009    20020131    1    900000000000207008    900000000000074008
113004    20020131    1    900000000000207008    900000000000074008
114005    20020131    1    900000000000207008    900000000000074008
115006    20020131    1    900000000000207008    900000000000074008
116007    20020131    1    900000000000207008    900000000000074008
117003    20020131    1    900000000000207008    900000000000074008
118008    20020131    1    900000000000207008    900000000000074008
119000    20020731    1    900000000000207008    900000000000073002
120006    20020131    1    900000000000207008    900000000000074008
121005    20020131    1    900000000000207008    900000000000074008 

这是代码

#include <iostream>
#include <vector>
#include <fstream>
#include <unordered_map>

using namespace std;

struct concept
{
    string id,effectiveTime,active,moduleId,definitionStatusId;
};

int main ()
{
    unordered_map<string,vector<concept>> concepts;
    ifstream conceptStream("concept.txt");
    if (!conceptStream.is_open())
    {
        cout << "Failed to open\n";
        return 0;
    }
    string id,effectiveTime,active,moduleId,definitionStatusId;
    string conceptLine;
    getline(conceptStream, conceptLine);
    while (!conceptStream.eof())
    {
        getline(conceptStream, id, '\t');
        getline(conceptStream, effectiveTime, '\t');
        getline(conceptStream, active, '\t');
        getline(conceptStream, moduleId, '\t');
        getline(conceptStream, definitionStatusId, '\n');
        concepts[id] = {{id,effectiveTime,active,moduleId,definitionStatusId}};
    }

  return 0;
}

您使用 find 搜索值。如果返回的迭代器不等于 end().

,则您有一个值
unordered_map<string,vector<concept>>::const_iterator it = concepts.find(id);
if (it != concepts.end()) {} //You have a value

您插入的向量将始终有一个条目,因此您可以取消它,但既然已经有了它,您就需要获取向量的第一个元素。

if (it != concepts.end())
  cout << it->second[0].id << it->second[0].effectiveTime; //etc

当然,您可能打算查看该 ID 是否已存在,如果存在,则将其添加到向量中。这会起作用,但也许您应该按照 Shawn 的建议考虑 std::unordered_multimap,如果不是,请将键类型更改为 concept,因为向量中只会有一个元素。

如果您删除矢量,访问代码将变得更简单:

if (it != concepts.end())
  cout << it->second.id << it->second.effectiveTime; //etc