更新时间:2023-02-09 21:15:49
您好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