为什么这两个字符串不相等,我该如何使其相等?
我试图弄清楚为什么在使用分配给 Dictionary[] 的键时能够在 Trie 中成功找到字符串,但是当我尝试创建具有相同值的完全不同的字符串时,我无法返回成功的搜索。当试图找出字符串不相同的原因时,我创建了一小段代码来尝试解决问题。我使用 key1.compare(key) 来显示字符串不相等,而编译后的代码显示两个字符串看起来相同。我最好的猜测是,这与每个字符串具有不同的指针有关。解决此问题后,我的最终目标是最终能够根据字符输入创建字符串,并搜索包含字典的 Trie 数据结构,看看我是否创建了一个单词。
当我搜索字典数组中的字符串时,我的特里树可以工作(该数组用于加载特里树),但当我使用原始数组之外的任何其他字符串时,我的特里树不起作用。
复制和粘贴的代码:
string key = dictionary[1];
cout << head->search(key) << " ";
string key1 = "the";
cout << head->search(key1) << " ";
cout<< endl<<key<<endl;
cout<< key1<<endl;
if(key1.compare(key)!=1){
cout<<"not equal"<<endl;
}
编译器: 1 0 这 这 不等于
I'm trying to figure out why I am able to successfully find strings in my Trie when using keys assigned to Dictionary[] but when I try to create an entirely different string of the same value, I am unable to return a successful search. When trying to figure out why the strings are not the same I created a small block of code to try and troubleshoot the problem. I used key1.compare(key) to show that the strings are not equal, while the compiled code shows that the two strings appear the same. My best guess is that this has something to do with each string having different pointers. My end goal after fixing this issue is to eventually be able to create strings based on an input of characters and search through my Trie data structure containing my dictionary to see if I created a word.
My trie works when I search for strings that are in my dictionary array (this array was used to load the trie), but don't work for when I use any other string outside the original array.
block of code used to test strings
Code copied and pasted:
string key = dictionary[1];
cout << head->search(key) << " ";
string key1 = "the";
cout << head->search(key1) << " ";
cout<< endl<<key<<endl;
cout<< key1<<endl;
if(key1.compare(key)!=1){
cout<<"not equal"<<endl;
}
Compiler:
1 0
the
the
not equal
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论