Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 760895
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:46:21+00:00 2026-05-14T15:46:21+00:00

I have this: typedef string domanin_name; And then, I try to overload the operator<

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:46:22+00:00Added an answer on May 14, 2026 at 3:46 pm

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I have a struct like this typedef struct { string firstname; string lastname;
I have this snippet of C code: #include<stdio.h> #include<stdlib.h> #include<string.h> typedef struct Date {
I have a struct like this: typedef struct string { unsigned long length; unsigned
In sample code, I have seen this: typedef enum Ename { Bob, Mary, John}
Ok so I have struct like this typedef struct { float x; float y;
I have this function signature I have to match typedef int (*lua_CFunction) (lua_State *L);//target
I have a struct like this typedef struct bookStruct { char title[80]; char author[80];
I have a struct that I initialize like this: typedef struct { word w;
I have a struct that looks like this: typedef struct _my_struct { float first_vector[SOME_NUM][OTHER_NUM];
If I have a class like this: typedef union { __m128 quad; float numbers[4];

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.