运算符重载的递归问题

发布于 2024-08-30 23:51:55 字数 1341 浏览 2 评论 0原文

我有这个:

typedef string domanin_name;

然后,我尝试重载运算符<这样:

bool operator<(const domain_name & left, const domain_name & right){
    int pos_label_left = left.find_last_of('.');   
    int pos_label_right = right.find_last_of('.');

    string label_left = left.substr(pos_label_left);
    string label_right = right.substr(pos_label_right);
    int last_pos_label_left=0, last_pos_label_right=0;

    while(pos_label_left!=string::npos && pos_label_right!=string::npos){
        if(label_left<label_right) return true;
        else if(label_left>label_right) return false;

        else{
            last_pos_label_left = pos_label_left;
            last_pos_label_right = pos_label_right;

            pos_label_left = left.find_last_of('.', last_pos_label_left);
            pos_label_right = right.find_last_of('.', last_pos_label_left);

            label_left = left.substr(pos_label_left, last_pos_label_left);
            label_right = right.substr(pos_label_right, last_pos_label_right);
        }
    }
}

我知道重载运算符 < 是一种奇怪的方式,但我必须这样做。它应该做我想做的。这不是重点。

问题是它在这一行进入无限循环:

if(label_left<label_right) return true;

看起来它试图使用这个重载函数本身来进行比较,但是 label_left 是一个字符串,而不是域名字!

有什么建议吗?

I have this:

typedef string domanin_name;

And then, I try to overload the operator< in this way:

bool operator<(const domain_name & left, const domain_name & right){
    int pos_label_left = left.find_last_of('.');   
    int pos_label_right = right.find_last_of('.');

    string label_left = left.substr(pos_label_left);
    string label_right = right.substr(pos_label_right);
    int last_pos_label_left=0, last_pos_label_right=0;

    while(pos_label_left!=string::npos && pos_label_right!=string::npos){
        if(label_left<label_right) return true;
        else if(label_left>label_right) return false;

        else{
            last_pos_label_left = pos_label_left;
            last_pos_label_right = pos_label_right;

            pos_label_left = left.find_last_of('.', last_pos_label_left);
            pos_label_right = right.find_last_of('.', last_pos_label_left);

            label_left = left.substr(pos_label_left, last_pos_label_left);
            label_right = right.substr(pos_label_right, last_pos_label_right);
        }
    }
}

I know it's a strange way to overload the operator <, but I have to do it this way. It should do what I want. That's not the point.

The problem is that it enter in an infinite loop right in this line:

if(label_left<label_right) return true;

It seems like it's trying to use this overloading function itself to do the comparision, but label_left is a string, not a domain name!

Any suggestion?

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

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

发布评论

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

评论(3

是伱的 2024-09-06 23:51:55

typedef 只是为类型提供另一个名称。它创建独特类型。因此,实际上,您正在重载 stringoperator <

如果您想创建一种独特的类型,那么您可以尝试

struct domain_name {
   string data;
   // ...
};

使用它。

typedef just gives another name for a type. It does not create a distinct type. So in effect, you're overloading operator < for string.

If you want to create a distinct type, then you can try

struct domain_name {
   string data;
   // ...
};

and work with that.

雪落纷纷 2024-09-06 23:51:55

Typedef 不是这样工作的。 Typedef 只是定义了该类型的别名 - 它仍然是一个字符串。为了做到这一点,您需要一个新的类型。无论如何你都应该这样做。您的运算符正在重载所有字符串的比较运算符。

Typedef doesn't work like this. Typedef simply defines an alias for the type - it is still a string. In order to do this, you would need a new type instead. You should do this anyway. Your operator is overloading the comparison operator for all strings.

行至春深 2024-09-06 23:51:55

您的 typedef 不会创建新类型。它只是创建一个新名称来引用与以前相同的类型。因此,当您在两个字符串的运算符函数内使用 < 时,编译器仅使用正在编译的相同运算符,因为参数类型匹配。

您可能希望做的是定义一个全新的函数:

bool domain_less(domain_name const& left, domain_name const& right);

然后在调用比较函数的地方使用该函数,例如 std::sort。大多数标准算法默认使用 <,但允许您提供自己的谓词函数。您可能需要使用 std::ptr_fun 来包装您的函数。您还可以编写自己的函子对象;在这种情况下,通常是从 std::binary_function 下降的。 (查看 标头。)

Your typedef doesn't create a new type. It just creates a new name to refer to the same type as before. Thus, when you use < inside your operator function on two strings, the compiler just uses the same operator it's compiling because the argument types match.

What you may wish to do instead is define an entirely new function:

bool domain_less(domain_name const& left, domain_name const& right);

Then use that function in places that call for a comparison function, such as std::sort. Most of the standard algorithms will use < by default, but allow you to provide your own predicate function instead. You may need to use std::ptr_fun to wrap your function. You can also write your own functor object; it's typical to descend from std::binary_function in that case. (Check out the <functional> header.)

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