Qt C++macOS问题。我正在使用函数 .find( "a word") 在多集中搜索单词,它适用于 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

本文关键字:适用于 搜索 集中 mac 单词 Windows 不适用 问题 C++macOS Qt find      更新时间:2023-10-16

我写了一些代码来加载一些包含单词列表的文件(一个单词pr行)。 每个单词都添加到多集。 后来我尝试使用 multiset.find("aWord") 搜索多集。 我在多集中查找单词和单词的子字符串。

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

但是如果我在我的 mac 上用 qt 编译它,那就不起作用了!

我的目标是让它在我的Mac上从Qt工作。

我在 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 

我试图打印出来: 我正在寻找的每个字符串 以及我应该在多集中找到的每个字符串作为十六进制格式 并得出结论,有些字母不匹配。 在那里十六进制值。尽管我认为我的整个系统都运行 UTF-8,并且该文件也是 UTF-8 编码的。

词典.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

字典.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;
}
}
}

主.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);

然后.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;

并在词典的结果输出中添加了相同的打印.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上却不是这样。我不知道我的环境中是否有任何编码设置需要更改或设置正确。

我想让我的主要.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。