C++:如何为 char 数组创建比较函数?

发布于 2024-10-11 15:29:52 字数 704 浏览 2 评论 0原文

这可能吗?当我将 char 作为类型时,我收到奇怪的错误消息:

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

错误消息:错误 C2803:'operator ==' 必须至少有一个类类型的形式参数 ...我根本不明白。

我在想我是否可以直接比较像:

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

然后比较:

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

等的东西。

但我也希望它能够与:

char *str = new char[100];

和:

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

我假设我用这种方式使用的每个字符数组都会以NULL字符结尾,所以检查应该是可能的,但我知道这可能不安全等。我只是想知道这是否可以做到,以及如何做到。

Is this possible? i get weird error message when i put char as the type:

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

柳絮泡泡 2024-10-18 15:29:52

这是不可能的。正如编译器所指出的,您不能为原始数据类型重载此运算符。比较的至少一侧必须是非原始的,才能实现重载。

同样的意义,您不能从原始数据类型派生新类(以向其添加功能)。

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).

两相知 2024-10-18 15:29:52

您正在尝试比较两个指针。

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

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

但是,您已经提供了 C++ 标记,因此您应该使用 std::string

#include <string>

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

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

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:

#include <string>

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

if(str1 == str2)  // yes!  std::string overloads operator ==
{
}
叶落知秋 2024-10-18 15:29:52

指针是内置类型。它们已经有内置的比较运算符,您无法覆盖它们。只需使用 std::string 即可。

Pointers are built-in types. There are built-in comparison operators for them already, you cannot override them. Just use std::string.

旧伤慢歌 2024-10-18 15:29:52

您当然完全可以编写自己的比较函数,事实上这是要做的最基本的事情之一。我不知道为什么有人会说你不能。您只是不能将其称为operator =。这是(未经测试):

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 ==.

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 =. 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 ==.
泪是无色的血 2024-10-18 15:29:52

使用 strcmp() 比较字符:它适用于字符指针 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;
}

Use strcmp() to compare chars: It works on char pointers char * and char arrays 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;
}
别闹i 2024-10-18 15:29:52

决定我的评论作为答案会更好,您应该使用标准 string 函数为此(strncmp、strncat 等)。

编辑:
正如另一个答案所指出的,你不能超载。但对于 char 数组和 char 指针,您应该使用标准库函数。

Decided that my comment would be better as an answer, you should use the standard string functions for this (strncmp, strncat, etc).

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文