简体   繁体   English

C ++:如何为char数组制作比较函数?

[英]C++: How to make comparison function for char arrays?

Is this possible? 这可能吗? i get weird error message when i put char as the type: 当我把char作为类型时,我得到奇怪的错误消息:

inline bool operator==(const char *str1, const char *str2){
    // ...
}

Error message: error C2803: 'operator ==' must have at least one formal parameter of class type ... which i dont understand at all. 错误消息: error C2803: 'operator ==' must have at least one formal parameter of class type ...我根本不明白。

I was thinking if i could directly compare stuff like: 我在想是否可以直接比较像:

const char *str1 = "something";
const char *str2 = "something else";
const char str3[] = "lol"; // not sure if this is same as above

and then compare: 然后比较:

if(str1 == str2){
   // ...
}

etc. 等等

But i also want it to work with: 但我也希望它能与之合作:

char *str = new char[100];

and: 和:

char *str = (char *)malloc(100);

I am assuming every char array i use this way would end in NULL character, so the checking should be possible, but i understand it can be unsafe etc. I just want to know if this is possible to do, and how. 我假设我使用这种方式的每个char数组都会以NULL字符结束,所以检查应该是可能的,但我知道它可能是不安全的等等。我只是想知道这是否可行,以及如何。

It is not possible. 这不可能。 As your compiler points out, you cannot overload this operator for primitive data types. 正如您的编译器指出的那样,您不能为原始数据类型重载此运算符。 At least one side of the comparison must be non-primitive for the overload to be possible. 比较的至少一侧必须是非原始的,以使过载成为可能。

In the same sense, you cannot derive a new class from a primitive data type (to add functionality to it). 同样,您无法从原始数据类型派生新类(向其添加功能)。

You are attempting to compare two pointers. 您正在尝试比较两个指针。

const char* str1 = "string1";
const char* str2 = "string1";

if(str1 == str2) // never true, str1 is not the same pointer as str2
{
};

But, you've provided the C++ tag, so you should be using std::string : 但是,您已经提供了C ++标记,因此您应该使用std::string

#include <string>

std::string str1 = "string1";
std::string str2 = "string1";

if(str1 == str2)  // yes!  std::string overloads operator ==
{
}

Pointers are built-in types. 指针是内置类型。 There are built-in comparison operators for them already, you cannot override them. 已经有内置的比较运算符,你不能覆盖它们。 Just use std::string . 只需使用std::string

Decided that my comment would be better as an answer, you should use the standard string functions for this (strncmp, strncat, etc). 决定我的评论会更好作为答案,你应该使用标准的字符串函数(strncmp,strncat等)。

Edit: As pointed out in another answer, you can't do the overload. 编辑:正如另一个答案所指出的,你不能做过载。 But in the case of the char arrays and char pointers you should use the standard library functions. 但是在char数组和char指针的情况下,您应该使用标准库函数。

You most certainly, totally can write your own comparison function, and in fact it's one of the most basic things to do. 你当然可以完全编写自己的比较函数,事实上它是最基本的事情之一。 I have no idea why someone would say that you can't. 我不知道为什么有人会说你做不到。 You just can't call it operator = . 你只是不能称之为operator = Here it is (untested): 这是(未经测试):

int my_strcmp(char const* s1, char const* s2)
{
  for(;*s1 == *s2 && *s1; ++s1,++s2);

  return *s1 - *s2;
}

...
if (!my_strcmp(str1, str2)) // they're ==.

Use strcmp() to compare chars: It works on char pointers char * and char arrays char [ ] . 使用strcmp()来比较字符:它适用于char指针char *和char数组char [ ]

#include <string.h>
#include <stdio.h>

char *string_Char_pointer1 = {"2012-12-06 14:28:51"};
char *string_Char_pointer2 = {"1911-12-06 14:28:51"};

char string_Char_Array1[] = "2012-12-06 14:28:51";
char string_Char_Array2[] = "1911-12-06 14:28:51";

int main( void )
{
   char tmp[20];
   int result;

   printf( "Comparing  string_Char_pointer..\n\n\n");

   printf( "Compared strings:\n   %s\n   %s\n\n\n", string_Char_pointer1, string_Char_pointer2 );
   result = strcmp( string_Char_pointer1, string_Char_pointer2 );

   if( result > 0 )        strcpy( tmp, "greater than" );
   else if( result < 0 )   strcpy( tmp, "less than" );
   else    strcpy( tmp, "equal to" );

   printf( "   strcmp:   String 1 is %s string 2\n\n", tmp );

   printf( "\n\nComparing string_Char_Array..\n\n");

   printf( "Compared strings:\n   %s\n   %s\n\n\n", string_Char_Array1, string_Char_Array2 );
   result = strcmp( string_Char_pointer1, string_Char_pointer2 );

   if( result > 0 )        strcpy( tmp, "greater than" );
   else if( result < 0 )   strcpy( tmp, "less than" );
   else    strcpy( tmp, "equal to" );




   return 0;
}

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

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