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 9156511
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:47:33+00:00 2026-06-17T12:47:33+00:00

Right now I have a class to do a binary search. The class accepts

  • 0

Right now I have a class to do a binary search. The class accepts a vector, but then I tell the class to sort.

I need to be able to have it sort by only first name potentially or last name, so I set a character argument as a choice in that class to change how I sort the vector. I also in that class made an operator() function to use *this, as a class pointer to sort the vector. But it seems to just be looping forever. Can anyone tell me why? Code Below.

*note if there’s some general practices I’m not following feel free to inform me. I don’t want to start making bad habits now.

By request: Getname

void personType::getName(string& first, string& last)
{
    // get the name and set it
    first = firstName;
    last = lastName;
}


bool sBinary::operator()(studentType student1, studentType student2){
    string toCheck1, toCheck2, fName1,lName1 ,fName2 , lName2;
    student1.getName(fName1, lName1);
    student2.getName(fName2, lName2);
    toCheck1=checkStr(fName1, lName1);
    toCheck2=checkStr(fName2,lName2);
    return toCheck1<toCheck2;
}

string sBinary::checkStr(string fName, string lName){
    string toCheck;
    switch (choice){
    case 'f':
    case 'F':
        toCheck=fName;
        break;
    case 'l':
    case 'L':
        toCheck=lName;
        break;
    case 'r':
    case 'R':
        toCheck=fName+lName;
        break;
    default:
        toCheck=lName+fName;

    }

    return toCheck;

}


sBinary::sBinary(vector<studentType> _sList, char _choice){
    sList=_sList;
    steps=0;
    choice=_choice;
    sort(sList.begin(),sList.end(), *this);
}
  • 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-06-17T12:47:34+00:00Added an answer on June 17, 2026 at 12:47 pm

    So, it seems not not to loop forever, but executes too long. It’s completely different story.
    You have a couple of pessimisations in your code:
    The main concern is that you pass *this, to the sorting algorithm:

    sort(sList.begin(),sList.end(), *this);
    

    std::sort takes comparation predicate by value and it copies it many times. You can see it, if you define copy constructor:

    sBinary(const sBinary& r):choice(r.choice), sList(r.sList)
    {
        std::cout << "copied\n";
    }
    

    And your vector gets copied along with the object itself.

    For example, if the array size is 200, std::sort copies object 13646 times. It means, that 2700000 student copy operations involved.

    So, you should not pass *this to std::sort. You’d better define static function lessThen instead of operator() and pass it to sorting algorithm.

    Further improvements:

    1. Pass by reference, rather then by value. For example, in your lessThen function declaration should look like

      static bool lessThen(const studentType& student1, const studentType& student2);
                         //^^^^^            ^
                         //constant         reference
      
    2. Refactor your studentType class.

      You’d better have 2 separate functions, returning first and last name (by constant reference). In this case you could get rid of copying names to temporary variables. Note, that when you have single function, you have to copy both first and last name, even if one name will never be used:

      const std::string& first_name() const { return _fname; }
      const std::string& last_name() const { return _lname; }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now I have class MyParamClass { all the parameters I need to pass
Right now I have a function, in a class that is used to listen
I'm in a mobile programming class right now and we have to make a
I have create my own NSOpenGLView class, right now the data that i want
I have always been kind of confused by threads, and my class right now
So right now I have a class called Clans which list several different clans
Right now I have some class myService extends Service() being called by a startService
In binary search tree we have the node strucure as class Node { public:
I don't have the code available right now, but I'll describe my situation and
So right now I have a class, called Set, in C++. In it, I

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.