vendredi 19 avril 2019

File data read to map, but map::find() doesn't work

I need to find a key/value pair in a map, but due to how the map is populated, for some reason map::find() doesn't work.

The data it reads from can be boiled down to

Key1,a,b,c
Key2,a
Key3
Key4,a,b

With this, I could print out all this data using an iterator loop, but map::find() does not work with Key3 or Key4.

Data is gathered from the file as such

file.open("input.txt");
std::string line;
std::string key;
while(getline(file,line,','))
{
    key = line;
    getline(file,line);
    m[key]=line;
}

Which can be printed with

for(auto it = m.begin(); it != map.end(); ++it)
{
    std::cout<<it->first<<","<<it->second<<std::endl;
}

Output

Key1,a,b,c
Key2,a
Key3
Key4,a,b

But for whatever reason, Key3 and Key4 don't exist according to map::find()

I've tried checking error bits, manually setting the value for Key3, nothing seems to work that allows me to use map::find() for values after Key3. (map::find() returns map.end() for those values)

I'm almost certain it's because of how I gather the data, specifically because Key3 doesn't have a comma, but I have to leave it in that form.

Aucun commentaire:

Enregistrer un commentaire