簡體   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