使用“strcmp”关于 c 中字符数组的特定成员
我有一个二分搜索函数,我传递一个指针字符数组、该数组的长度、一个搜索指针字符数组和另一个用于其他内容的计数器。
int binarySearch(char* charArray, int len, char* searchItem, int counter)
{
int position;
int begin = 0;
int end = len-1;
int cond =0;
while(begin <= end)
{
position = (begin + end)/2;
// searchItem is a pointer array and the value I want to compare to is
// at the index of counter (determined outside of this function)
if((cond = strcmp(&charArray[position], &searchItem[counter])) == 0)
{
return position;
}
else if(cond < 0){
begin = position + 1;
}
else
end = position - 1;
}
return -1;
}
从这里开始,手动浏览代码似乎让我认为它应该可以正常工作,但事实并非如此。我认为我在我的指针以及我如何引用它们的地方被抛弃了,所以比较了错误的数据。
我已经看了太久了......这里真的需要一些帮助。
I have a binary search function I am passing a pointer character array, the length of that array, a search pointer character array and another counter for something else.
int binarySearch(char* charArray, int len, char* searchItem, int counter)
{
int position;
int begin = 0;
int end = len-1;
int cond =0;
while(begin <= end)
{
position = (begin + end)/2;
// searchItem is a pointer array and the value I want to compare to is
// at the index of counter (determined outside of this function)
if((cond = strcmp(&charArray[position], &searchItem[counter])) == 0)
{
return position;
}
else if(cond < 0){
begin = position + 1;
}
else
end = position - 1;
}
return -1;
}
From here, going through the code by hand seems to make me want to think it should work fine, however it doesn't. I think I'm getting thrown off somewhere along the lines of my pointers and how I'm referring to them so the wrong data is being compared.
I've looked at it for too long now... really need some help here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
目前还不清楚正在什么内容中进行搜索。但我猜测您正在排序的字符数组中搜索字符。如果是这种情况,则不能使用
strcmp
。相反,你可以这样做:It is not very clear what is being searched in what. But I'm guessing that you are searching for a character in a sorted character array. If that is the case, you can't use a
strcmp
. Instead you can do:strcmp
假定所比较的字符串以零结尾,并且长度完全相同。因此,strlen(&charArray[position])
必须等于strlen(&searchItem[counter])
。这意味着position == strlen(&charArray[0]) - strlen(&searchItem[counter])
。您根本不需要搜索。charArray
的后缀要么匹配,要么不匹配。但这可能不是您想要的。你想达到什么目的?
strcmp
assumes that the strings being compared are zero-terminated, and exactly equal length. Therefore,strlen(&charArray[position])
has to equalstrlen(&searchItem[counter])
. That meansposition == strlen(&charArray[0]) - strlen(&searchItem[counter])
. You don't need to search at all. Either the suffix ofcharArray
matches or it doesn't.But that's probably not what you intended. What are you trying to achieve?
要比较的字符串的长度是否完全相同?您的代码假设是这样。如果没有,您将需要使用 strncmp( ),而不是 strcmp()。
Are the strings to be compared all of the exact same length? Your code is assuming so. If not, you'll want to use strncmp( ), not strcmp().
strcmp 比较 char* 中直到尾随“\0”字符的所有字符。因此,您无法比较单个字符(基本上您总是需要两个字符,即字符和尾随的“\0”),并且无法比较字符串的各个部分,除非您在要执行的位置插入“\0”比较。
为了清楚起见,正确的以零结尾的字符串(最后一个字符是“\0”)对于 strcmp 很重要。 strcmp 比较两个字符数组(从开头到“\0”字符)并返回适当的比较值(<0、=0、>0)。当然,两个字符数组的长度必须相同。
strcmp compares all the characters in a char* up to the trailing '\0' character. So you cannot compare single characters (basically you always need two, the character and the trailing '\0') and you cannot compare parts of a string unless you insert a '\0' at the location up to which you want to perform the comparison.
Just for clarity, properly zero terminated strings (last character is '\0') are important for strcmp. strcmp compares two character arrays from the start up to the '\0' character and returns an appropriate comparison value (<0, =0, >0). And of course, both character arrays have to be the same length.
如果这些是 ASCII 字符串并且应该按字母顺序排序,我相信应该是
我不确定您希望如何对它们进行排序?
If these are ASCII strings and should be sorted in alphabetic order, I believe it should be
I'm not certain how you wish to sort them though?