異なるプラットフォームの STL マップで find() を使用すると問題が発生するようです。これが私のコードです。
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
using namespace std;
void constructDictionary(map<string,bool> &dict);
bool isInDictionary(string word, map<string,bool> &dict);
int main(void)
{
map<string, bool> dictionary;
constructDictionary(dictionary);
map<string, bool>::iterator it = dictionary.begin();
while(it != dictionary.end()){
cout << it->first <<endl;
it++;
}
string word;
while(true){
cout << "Enter a word to look up: " << endl;
cin >> word;
if(isInDictionary(word, dictionary))
cout << word << " exists in the dictionary." << endl;
else
cout << word << " cannot be found in the dictionary." << endl;
}
return 0;
}
void constructDictionary(map<string,bool> &dict)
{
ifstream wordListFile;
wordListFile.open("dictionaryList.txt");
string line;
while(!wordListFile.eof()){
getline(wordListFile, line);
dict.insert(pair<string,bool>(line, true));
}
wordListFile.close();
}
bool isInDictionary(string word, map<string,bool> &dict)
{
if(dict.find(word) != dict.end())
return true;
else
return false;
}
isInDictionary()
Windows で Visual Studio を使用してコンパイルした場合は正常に動作しますが、ubuntu および g++ では、これはマップに最後に作成されたエントリに対してのみ機能します。私が照会する他の単語はすべて false を返します。この動作の矛盾がわかりません。どちらの場合も、main の先頭にある while ステートメントは、マップ内のすべてを正しく出力して、すべてがそこにあることを証明します。
何か案は?ありがとうございました。