C 或 C++。如何比较给定char *指针的两个字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2330350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C or C++. How to compare two strings given char * pointers?
提问by user69514
I am sorting my array of car two ways. one by year which is shown below. and another one by make. Make is a char* How do I compare strings when I just have pointers to them?
我正在以两种方式对我的汽车阵列进行排序。如下所示。和另一个制造商。Make 是一个字符* 当我只有指向字符串的指针时,如何比较字符串?
int i, j;
for(i=0; i<100; i++){
for(j=0; j<100-i; j++){
if(carArray[i]!=NULL && carArray[j]!= NULL && carArray[j+1]!=NULL){
if(carArray[i]->year > carArray[j+1]->year){
swap(carArray[j], carArray[j+1]);
}
}
}
}
The above method works for int's (year). How can I make it work for char pointers?
上述方法适用于 int's (year)。我怎样才能使它适用于字符指针?
回答by T.E.D.
In pretty much either one, the wayis to call strcmp
. If your strings (for some weird reason) aren't NUL terminated, you should use strncmp
instead.
在几乎任何一种情况下,方法都是调用strcmp
. 如果您的字符串(出于某种奇怪的原因)未以 NUL 终止,则应strncmp
改用。
However, in C++ you really shouldn't be manipulating strings in char arrays if you can reasonably avoid it. Use std::string
instead.
但是,在 C++ 中,如果可以合理避免的话,您真的不应该操作 char 数组中的字符串。使用std::string
来代替。
回答by synepis
I think you need to use the strcmp() function.
我认为您需要使用 strcmp() 函数。
回答by Walt Stoneburner
Make sure the char * isn't null, and if you want, look for the stricmp() function for case insensitive comparisons. Otherwise, use strcmp().
确保 char * 不为空,如果需要,请查找 stricmp() 函数以进行不区分大小写的比较。否则,使用 strcmp()。
char * actually represents the memory address of the first character in each string. So you don't really want to be comparing the values of the pointers, but the contents they point to.
char * 实际上代表了每个字符串中第一个字符的内存地址。所以你真的不想比较指针的值,而是它们指向的内容。
回答by Milan
In C its strcmp() function as already stated. In C++ you can use the compare() function.
在 C 中,它的 strcmp() 函数如前所述。在 C++ 中,您可以使用 compare() 函数。
C:
C:
char str1[10] = "one";
char str2[10] = "two";
if (strcmp(s, t) != 0) // if they are equal compare return 0
C++
C++
string str1 ("one");
string str2 ("two");
if (str1.compare(str2) != 0) // if they are equal compare return 0
回答by Craig
I'm of course assuming here you have char * for the car makes
我当然假设这里你有汽车制造商的字符 *
int i, j;
for(i=0; i<100; i++){
for(j=0; j<100-i; j++){
if(carArray[i]!=NULL && carArray[j]!= NULL && carArray[j+1]!=NULL){
if(strcmp(carArray[i]->make, carArray[j+1]->make) == 0)
{
//Do whatever here
}
}
}
}
You want to compare against 0 because strcmp will return 0 if there is no difference between the two strings.
strcmp takes two const char *.
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
您想与 0 进行比较,因为如果两个字符串之间没有区别, strcmp 将返回 0。
strcmp 需要两个 const char *。
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/
回答by Scott Wolchok
You should really use qsort
(in C, #include <stdlib.h>
) or std::sort
(in C++, #include <algorithm>
) instead of a bubble sort like this. If it's C++ and you take @T.E.D.'s advice to use std::string
instead of raw C strings, you don't even have to specify a comparison because the <
operator will be used and will do the right thing.
你真的应该使用qsort
(in C, #include <stdlib.h>
) or std::sort
(in C++, #include <algorithm>
) 而不是像这样的冒泡排序。如果它是 C++ 并且您接受@TED 的建议std::string
而不是原始 C 字符串,则您甚至不必指定比较,因为<
将使用运算符并将执行正确的操作。
回答by AnT
When you need to compare two char pointersspecifically, you can compare them in the usual way: by using comparison operators <
, >
, ==
etc.
当你需要比较两个char指针而言,可以用通常的方式对它们进行比较:通过比较操作符<
,>
,==
等。
The issue in ths case is that you don'tneed to compare two char pointers. What you do need though, is to compare two C-style stringsthese char pointers are pointing to. In order to compare the C-style strings, you have to use the standard strcmp
function.
在部份情况下,问题是,你并不需要比较两个字符指针。但是,您确实需要比较这些字符指针指向的两个 C 样式字符串。为了比较 C 风格的字符串,您必须使用标准strcmp
函数。
On top of that, the approach to handling null elements in your sorting algorithm doesn't seem to make any sense whatsoever. Imagine an input array that contains alternating null pointers and non-null pointers. It is obvious, that your sorting algorithm will never sort anything, since the condition in your if
will never be true. You need to reconsider your handling of null elements. Of course, first of all, you have to decide what to do with them. Ignore and leave in place? Push to one end of the array? Somethng else?
最重要的是,在排序算法中处理空元素的方法似乎没有任何意义。想象一个包含交替空指针和非空指针的输入数组。很明显,您的排序算法永远不会对任何内容进行排序,因为您的条件if
永远不会为真。您需要重新考虑对空元素的处理。当然,首先,您必须决定如何处理它们。忽略并留在原地?推到数组的一端?还有什么?
回答by alexis
strcmp() will help you to compare two char* http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp
strcmp() 将帮助您比较两个字符* http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp