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

问题描述 投票:0回答:1

我正在做一个作业,其中给了我一个大的制表符分隔值txt文件,我必须使用一个良好的数据结构来搜索它。现在我面临的问题是我不知道我是否正确地将值插入此unordered_map中,键是字符串,而值是struct的向量。其次,如果我想搜索它将如何做?例如,如果我输入第一个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;
}
c++ data-structures hashtable lookup unordered-map
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.