搜索二进制搜索树时,std :: string :: find()返回-1 [英] std::string::find() returns -1 when searching binary search tree

查看:83
本文介绍了搜索二进制搜索树时,std :: string :: find()返回-1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有两个字符串,其中一个名为 " strIn"。另一个是"叶子"上的属性。 - 为了简洁起见,除了我的代码之外,我将把它称为strOut。


我试图看看strOut是否以strIn开头。也就是说,如果strOut是"KAREN",那么和strIn是"KAR",然后find()应该返回0.但是,它返回-1


这是我的代码:

 string bTreeClass :: Search(string strIn)
{
string strOut;

//组织二叉树,使得下面的叶子在左边
//从根叶开始(当前叶子是根叶子
LeafClass * currentLeaf = rootLeaf ;
//循环直到找到匹配或当前叶没有链接
而(true)
{
int find = currentLeaf-> GetString()。find(strIn);
//如果strIn在currentLeaf的字符串
if(currentLeaf-> GetString()。find(strIn)== 0)
//匹配找到
//退出循环
break;
//否则,如果搜索数据小于当前叶数据
else if(strIn.compare(currentLeaf-> GetString())< 0){
//如果当前叶子没有左节点
if(currentLeaf-> GetLeftLink()== nullptr)
//匹配失败
//退出循环
break;
//其他当前叶子确实有一个左节点
else
//将当前叶子前进到当前叶子的左节点
currentLeaf = currentLeaf-> GetLeftLink();
//恩d如果
}
// Else(搜索数据大于当前叶子数据)
else {
//如果当前叶子没有正确的节点
如果(currentLeaf-> GetRightLink()== nullptr)
//匹配失败
//退出循环
break;
//其他当前叶子确实有一个正确的节点
else
//将当前叶子前进到当前叶子的右边节点
currentLeaf = currentLeaf-> GetRightLink();
//结束如果
//结束如果
}
//结束循环
}
strOut = currentLeaf-> GetString();
返回strOut;
}


解决方案

您好montana.burr,


感谢您在此发帖。

根据您的描述。我在C ++项目中测试find函数。

我的测试结果为0,使用find函数匹配字符串。

这是我的测试代码。

 #include< iostream> // std :: cout 
#include< string> // std :: string

int main()
{
std :: string str("带有针的干草堆中的针头。");
std :: string str2(" needle");

// find的不同成员版本与上面的顺序相同:
std :: size_t found = str.find(str2);
if(found!= std :: string :: npos)
std :: cout<< "first'pin'发现于:" <<发现<< "\\\
";

//让我们替换第一根针:
str.replace(str.find(str2),str2.length()," preposition");
std :: cout<< str<< "\\\
";

返回0;
}

我对你的问题感到好奇,请你为我们提供一个复制演示? / p>

关于LeafClass类,我认为这个课应该由你自己实现,你能提供关于课程的更多信息吗?


我希望回复对你有所帮助。


最好的问候,

Hart


Hi,

I have two strings, one of which is named  "strIn". The other is a property on a "leaf" - for brevity, I'll call this one strOut except in my code.

I'm trying to see if strOut begins with strIn. That is, if strOut is "KAREN" and strIn is "KAR", then find() should return 0. However, it instead returns -1

Here is my code:

string bTreeClass::Search(string strIn)
{
	string strOut;
	
	// Binary trees are organized so that the lower leaf is on the left
	// Start with the root leaf(current leaf is the root leaf
	LeafClass *currentLeaf = rootLeaf;
	//	Loop until match found or current leaf has no links
	while (true)
	{
		int find = currentLeaf->GetString().find(strIn);
		//	If strIn is in currentLeaf's string
		if (currentLeaf->GetString().find(strIn) == 0)
			// Match found
			// Exit Loop
			break;
		//	 Else if search data is less than current leaf data
		else if (strIn.compare(currentLeaf->GetString()) < 0) {
			//	If current leaf does not have a left node
				if (currentLeaf->GetLeftLink() == nullptr)
					//	Match failed
					//	Exit loop
					break;
			//	Else current leaf does have a left node
				else
					//	Advance current leaf to current leaf's left node
					currentLeaf = currentLeaf->GetLeftLink();
				//	End if
			}
		// Else (search data is greater than current leaf data)
		else {
			//	If current leaf does not have a right node
			 if (currentLeaf->GetRightLink() == nullptr)
					//	Match failed
					//	Exit loop
					break;
				//	Else current leaf does have a right node
				else
					//	Advance current leaf to current leaf's right node
					currentLeaf = currentLeaf->GetRightLink();
				//	End if
			//	End if
			}
		//	End loop	
		}
	strOut = currentLeaf->GetString();
	return strOut;
}

解决方案

Hi montana.burr ,

Thank you for posting here.
According to your description. I test find function within C++ project.
My test result is 0 by using find function to match string.
Here is my test code.

#include <iostream>       // std::cout
#include <string>         // std::string

int main ()
{
  std::string str ("needles in this haystack with needles.");
  std::string str2 ("needle");

  // different member versions of find in the same order as above:
  std::size_t found = str.find(str2);
  if (found!=std::string::npos)
    std::cout << "first 'needle' found at: " << found << '\n';

  // let's replace the first needle:
  str.replace(str.find(str2),str2.length(),"preposition");
  std::cout << str << '\n';

  return 0;
}

I'm curious about your issue, could you please provide a reproducing demo for us?

about the LeafClass class, I think the class should be achieved by yourself, Could you provide more information about the class?

I hope the reply would be helpful for you.

Best Regards,
Hart


这篇关于搜索二进制搜索树时,std :: string :: find()返回-1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆