C++ STL Map - find(pair) 在 Windows Visual Studio 中工作,但不适用于 g++在Linux中!

发布于 2024-10-19 13:04:21 字数 1714 浏览 3 评论 0原文

我似乎在不同平台上使用 find() 和 STL 地图时遇到问题。这是我的完整代码:

#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;
}

如果在 Windows 中使用 Visual Studio 编译,isInDictionary() 工作正常,但是,在 ubuntu 和 g++ 上,这只适用于映射中的最后一个条目。我查询的任何其他单词都会返回 false。我不明白这种行为的差异。在这两种情况下,main 开头的 while 语句正确打印出映射中的所有内容,以证明所有内容都在那里。

有什么想法吗?谢谢。

I seem to be having issues with using find() with the STL map on different platforms. Here is my code to be complete:

#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() works fine if compiled using visual studio in windows, however, on ubuntu and g++, this only works for the last entry made into the map. Any other word that I query returns false. I don't understand the discrepancy in this behavior. In both cases, the while statement at the beginning of main correctly prints out everything in the map to prove that everything's there.

Any ideas? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

帥小哥 2024-10-26 13:04:21
  • while (!eof) 是错误的。使用 while (getline(...))
  • 您需要处理 Windows 换行 \r\n。可能您的字典是在 Windows 上生成的,并且最后一行没有换行符,因此除最后一个之外的所有单词末尾都有一个隐藏的 \r
  • while (!eof) is wrong. Use while (getline(...))
  • You need to handle the windows linefeed \r\n. Likely your dictionary is generated on windows, and the last line doesn't have a linefeed, so all words except the last has a hidden \r at the end.
帥小哥 2024-10-26 13:04:21

您的输入文件中的 getline 和 line-endings 是否有错误?您可能会发现在 Linux 上它为每个单词添加了一个额外的 \r

假设你的单词都不包含空格,你可以通过简单地使用来解决这个问题:

std::string word;
while( wordListFile >> word )
{
   if( !word.empty() )
   {
       // do the insert
   }
}

你也可以使用 getline 但“修剪”两端的字符串。不幸的是没有标准的修剪功能。有一些实现。

您可能应该使用 std::set 作为集合类型,而不是这个额外的“bool”,只要有条目,它就始终为 true。

Is the error in getline and line-endings in your input file? You might find on Linux it is adding an extra \r to each word.

Assuming none of your words contain spaces, you can get around this by simply using:

std::string word;
while( wordListFile >> word )
{
   if( !word.empty() )
   {
       // do the insert
   }
}

You can also use getline but "trim" the strings at either end. There is unfortunately no standard trim function. There are a few implementations around.

You should probably use std::set as your collection type instead of this extra "bool" which is always true whenever there is an entry.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文