简体   繁体   English

C 或 C++。 如何比较给定char *指针的两个字符串?

[英]C or C++. How to compare two strings given char * pointers?

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).上述方法适用于 int's (year)。 How can I make it work for char pointers?我怎样才能使它适用于字符指针?

In pretty much either one, the way is to call strcmp .在几乎任何一种情况下,方法都是调用strcmp If your strings (for some weird reason) aren't NUL terminated, you should use strncmp instead.如果您的字符串(出于某种奇怪的原因)未以 NUL 终止,则应改用strncmp

However, in C++ you really shouldn't be manipulating strings in char arrays if you can reasonably avoid it.但是,在 C++ 中,如果可以合理避免的话,您真的不应该操作 char 数组中的字符串。 Use std::string instead.改用std::string

我认为您需要使用 strcmp() 函数。

Make sure the char * isn't null, and if you want, look for the stricmp() function for case insensitive comparisons.确保 char * 不为空,如果需要,请查找 stricmp() 函数以进行不区分大小写的比较。 Otherwise, use strcmp().否则,使用 strcmp()。

char * actually represents the memory address of the first character in each string. char * 实际上代表了每个字符串中第一个字符的内存地址。 So you don't really want to be comparing the values of the pointers, but the contents they point to.所以你真的不想比较指针的值,而是它们指向的内容。

In C its strcmp() function as already stated.在 C 中,它的 strcmp() 函数如前所述。 In C++ you can use the compare() function.在 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

strcmp() 将帮助您比较两个字符* http://www.cplusplus.com/reference/cstring/strcmp/?kw=strcmp

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.您想与 0 进行比较,因为如果两个字符串之间没有区别, strcmp 将返回 0。
strcmp takes two const char *. strcmp 需要两个 const char *。
http://www.cplusplus.com/reference/clibrary/cstring/strcmp/ http://www.cplusplus.com/reference/clibrary/cstring/strcmp/

You should really use qsort (in C, #include <stdlib.h> ) or std::sort (in C++, #include <algorithm> ) instead of a bubble sort like this.你真的应该使用qsort (在 C 中, #include <stdlib.h> )或std::sort (在 C++ 中, #include <algorithm> )而不是像这样的冒泡排序。 If it's C++ and you take @TED'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.如果它是 C++ 并且你接受@TED 的建议使用std::string而不是原始 C 字符串,你甚至不必指定比较,因为<运算符将被使用并且会做正确的事情。

When you need to compare two char pointers specifically, you can compare them in the usual way: by using comparison operators < , > , == etc.当您需要专门比较两个字符指针时,您可以用通常的方式比较它们:使用比较运算符<>==等。

The issue in ths case is that you don't need to compare two char pointers.在部份情况下的问题是,你并不需要比较两个字符指针。 What you do need though, is to compare two C-style strings these char pointers are pointing to.但是,您需要做的是比较这些字符指针指向的两个 C 样式字符串 In order to compare the C-style strings, you have to use the standard strcmp function.为了比较 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.很明显,您的排序算法永远不会对任何内容进行排序,因为if的条件永远不会为真。 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?还有什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM