mercredi 6 juin 2018

Reading from a file into a map which has key as integers and corresponding values as set

I have entries in a txt file in the form of:

329881 100000005 100000125 100000145 

390876 100000667 100000745 100000288 100000395 100000392

289456 100034560 100000145 100000286 100000245

I want to read this txt file,structure by structure into a map which has 2 fields:

struct keyvaluestruct
{
    uint64_t _key;
    unordered_set<uint64_t> _set;
};

where key for 1st struct should be 329881 and the set should be 100000005 100000125 100000145.Similarly,for the 2nd struct the key should be 390876 and values in the set should be 100000667 100000745 100000288 100000395 100000392. These structures are then inserted into a map

map < uint64_t,unordered_set< uint64_t>> _mapstructs;

I am following the approach:

keyvaluestruct s1;

ifstream ifile('file.txt')

while(!ifile.eof())
{
    ifile>>key>>vset;

    s1._key = key;

    s1._set = vset;

    _mapstructs.insert(s1._key,s1._set);
}

Can anyone help me in correcting this approach or suggest an alternate approach?

Aucun commentaire:

Enregistrer un commentaire