繁体   English   中英

Qt C ++ macOS问题。 我正在使用函数.find(“一个单词”)在multiset中搜索单词,它可以在Windows上运行,但不能在mac上运行

[英]Qt C++ macOS problem. I am searching for words in multiset with function .find(“a word”) it works on windows but not on mac

我编写了一些代码来加载一些包含单词列表的文件(一个单词pr行)。 每个单词都添加到multiset中。 后来我尝试用multiset.find(“aWord”)搜索multiset。 在那里我寻找multiset中单词的单词和子串。

如果我在Windows系统上用qt编译它,这段代码工作正常。

但是如果我在我的mac上用qt编译它就行不通!

我的目标是让它在我的Mac上从qt开始工作。

我正在使用Mac上的macbook Air(13“,早于2018年)

macOS Majave version 10.14.4 instalation
Buil version 18E226
local 18.5.0 Darwin Kernel Version 18.5.0: Mon Mar 11 20:40:32 PDT 
2019; root:xnu-4903.251.3~3/RELEASE_X86_64 x86_64

使用qt安装:QTKit:

  Version: 7.7.3
  Obtained from: Apple
  Last Modified: 13/04/2019 12.11
  Kind: Intel
  64-Bit (Intel): Yes
  Get Info String: QTKit 7.7.3, Copyright 2003-2012, Apple Inc.
  Location: /System/Library/Frameworks/QTKit.framework
  Private: No

和xcode安装:

  Xcode 10.2
  Build version 10E125 

我试图打印出来:我正在搜索的每个字符串以及我应该在multiset中找到的每个字符串都是十六进制格式,并断定某些字母不匹配。 在那里十六进制值。 尽管我认为我的整个系统运行utf-8并且文件也是utf-8编码的。

Dictionary.h

  #ifndef DICTIONARY_H
  #define DICTIONARY_H
  #include <iostream>
  #include <vector>
  #include <set>

  class Dictionary
  {
  public:
      Dictionary();
      void SearchForAllPossibleWordsIn(std::string searchString);

  private:
      std::multiset<std::string, std::less<std::string>> mDictionary;

      void Initialize(std::string folder);
      void InitializeLanguage(std::string folder, std::string languageFileName);
  };

  #endif // DICTIONARY_H

Dictionary.cpp

#include "Dictionary.h"
#include <vector>
#include <set>
#include <iostream>
#include <fstream>
#include <exception>

Dictionary::Dictionary()
{
    Initialize("../Lektion10Projekt15-1/");
}

void Dictionary::Initialize(std::string folder)
{
    InitializeLanguage(folder,"da-utf8.wl");
}

void Dictionary::InitializeLanguage(std::string folder, std::string languageFileName)
{
    std::ifstream ifs;

    ifs.open(folder+languageFileName,std::ios_base::in);
    if (ifs.fail()) {
        std::cerr <<"Error! Class: Dictionary. Function: InitializeLanguage(...). return: ifs.fail to load file '" + languageFileName + "'"  << std::endl;
    }else {
        std::string word;

        while (!ifs.eof()) {
            std::getline(ifs,word);

            mDictionary.insert(word);
        }
    }
    ifs.close();
}

void Dictionary::SearchForAllPossibleWordsIn(std::string searchString)
{
    std::vector<std::string> result;

    for (unsigned int a = 0 ; a <= searchString.length(); ++a) {
        for (unsigned int b = 1; b <= searchString.length()-a; ++b)     {

            std::string substring = searchString.substr(a,b);

            if (mDictionary.find(substring) != mDictionary.end())
            {
                result.push_back(substring);
            }
        }
    }

    if (!result.empty()) {
        for (unsigned int i = 0; i < result.size() ;++i) {
            std::cout << result[i] << std::endl;
        }
    }
}

main.cpp中

#include <iostream>
#include "Dictionary.h"

int main()
{
    Dictionary myDictionary;

    myDictionary.SearchForAllPossibleWordsIn("byggearbejderen");

    return 0;
}

我试图在main.cpp中更改以下行

    myDictionary.SearchForAllPossibleWordsIn("byggearbejderen");

to(OBS:单词列表中的第一个单词是byggearbejderen)

    std::ifstream ifs;
    ifs.open("../Lektion10Projekt15-1/da-utf8.wl",std::ios::in);
    if (ifs.fail()) {
        std::cerr <<"Error!" << std::endl;
    }else {
        std::getline(ifs,searchword);
    }
    ifs.close();
    myDictionary.SearchForAllPossibleWordsIn(searchword);

然后在main.cpp中添加som打印出来的预期字符串和子字符串的十六进制值。

    std::cout << " cout as hex test:" << std::endl;

    myDictionary.SearchForAllPossibleWordsIn(searchword);

    std::cout << "Suposet search resul for ''bygearbejderen''" << std::endl;

    for (char const elt: "byggearbejderen")
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
    std::cout << "byggearbejderen" << std::endl;

    for (char const elt: "arbejderen")
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
    std::cout  << "arbejderen" << std::endl;

    for (char const elt: "ren")
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
    std::cout  << "ren" << std::endl;

    for (char const elt: "en")
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
    std::cout  << "en" << std::endl;

    for (char const elt: "n")
        std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
    std::cout  << "n" << std::endl;

并且还在Dictonary.cpp的结果中添加了相同的打印件

std::cout << "result of seartchword as hex" << std::endl;
if (!result.empty()) {
    for (unsigned int i = 0; i < result.size() ;++i)
    {
        for (char const elt: result[i] )
        {
            std::cout << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(elt) << "  ";
        }
        std::cout  << result[i] << std::endl;


    }
}

它给出了以下输出:

result of seartchword as hex
ffffffef  ffffffbb  ffffffbf  62  79  67  67  65  61  72  62  65  6a  64  65  72  65  6e  0d  byggearbejderen
61  72  62  65  6a  64  65  72  65  6e  0d  arbejderen
72  65  6e  0d  ren
65  6e  0d  en
6e  0d  n
Suposet search resul for ''bygearbejderen''
62  79  67  67  65  61  72  62  65  6a  64  65  72  65  6e  00  byggearbejderen
61  72  62  65  6a  64  65  72  65  6e  00  arbejderen
72  65  6e  00  ren
65  6e  00  en
6e  00  n

我注意到一些值是不同的。 我不知道为什么在我使用macOS时会出现这种情况,但在Windows上并非如此。 我不知道在我的环境中是否有任何编码设置我需要更改或设置正确。

我想我的main.cpp看起来很喜欢这个:

#include <iostream>
#include "Dictionary.h"

int main()
{
    Dictionary myDictionary;

    myDictionary.SearchForAllPossibleWordsIn("byggearbejderen");

    return 0;
}

产生以下输出:

byggearbejderen
arbejderen
ren
en
n

文本文件的行结尾在Windows上与在Mac上不同。 Windows使用两个CR / LF字符(分别为ASCII代码13和10)。 旧Mac只使用CR字符,Linux系统只使用LF。 如果在Windows上创建文本文件,然后将其复制到Mac,则可能无法正确处理行结尾。

如果你查看输出中的最后一个字符,你会看到它是一个0d ,它将是CR字符。 我不知道你是如何生成那个输出的,但有可能Mac上的getline将其视为普通字符,并将其包含在已读入的字符串中。

最简单的解决方案是预先处理该文本文件以使行结束正确,或者在读入后从单词的末尾剥离CR。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM