lundi 29 juin 2015

c++ Map- Not finding keys that are present in a set

int createAcronymMap() {
string line;
string temp;
ifstream myfile("Q:\\USER\\acronymsList.txt");
if (myfile.is_open())
{
    while (getline(myfile, line))
    {
        char sep = ':';
        for (size_t p = 0, q = 0; p != line.npos; p = q)
        {
            v.push_back(line.substr(p + (p != 0), (q = line.find(sep, p + 1)) - p - (p != 0)));
        }
    }
    for (int i = 0;i < v.size() - 2;i = i + 2)
    {
        m[v[i]] = v[i + 1];
    }

    myfile.close();
    for (auto elem : m)
    {
        std::cout << elem.first << " " << elem.second.first << " " << elem.second.second << "\n";
    }
}

else cout << "Unable to open file";

return 0;
}
void write_document(string x)
{
istringstream buf(x);
istream_iterator<string> beg(buf), end;
vector<string> tokens(beg, end); // done!

for (auto& s : tokens)      cout << '"' << s << '"' << '\n';
ofstream myfile;

myfile.open("Q:\\USER\\acronyms.txt");
while (tokens.size() > 0)
{
    s.insert(tokens[0]);
    tokens.erase(tokens.begin());
}
for (auto f : s) {
    cout << m.count(f) << endl;
    if (m.count(f))
    {
        cout << "worked";
        myfile << f << ": " << m[f] << "\n";
    }
}

myfile.close();
}
vector<string>v;
map<string, string> m;
set<string> s;

This code reads a vector of strings, called tokens, that was created from the clipboard's contents(which are a word file's contents in this case), into a set of strings. It then creates a map from a .txt file that holds a list of acronyms and their definitions. The set is then checked against the map, and if an acronym is found in the map, writes the acronym and its definition to a new .txt file, such that a user can then copy/paste the list of acronyms. However, the map never returns a key as being found, and thus never writes anything to the output .txt file.

Aucun commentaire:

Enregistrer un commentaire