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

  • Home
  • SEARCH
  • 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 8373391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T14:39:54+00:00 2026-06-09T14:39:54+00:00

cmp bool cmp(const pair<string, long> &p1, const pair<string, long> &p2){ if(p1.second!=p2.second) return p1.second <

  • 0

cmp

bool cmp(const pair<string, long> &p1, const pair<string, long> &p2){
if(p1.second!=p2.second)
return p1.second < p2.second;
return strcmp(p1.first.c_str(),p2.first.c_str());
}

Hi all,

I’m trying to sort the vector based on the second element of the pair. If the second elements of the pair are equal, then I compare the first elements of the pair.

I’m using the above code to sort a vector containing string and int pair. I’m invoking the sorting function using sort_heap(vector.begin(),vector.end(),cmp);. But this doesn’t seem to work as expected.

  • 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-09T14:39:55+00:00Added an answer on June 9, 2026 at 2:39 pm

    Just use operator< for the strings:

    bool cmp(const pair<string, long> &p1, const pair<string, long> &p2)
    {
        if(p1.second!=p2.second)
            return p1.second < p2.second;
        return p1.first < p2.first;
    }
    

    strcmp returns a negative number if the first is “less than” the second (and that’s all you care about), 0 if they are equal, and a positive number if the second is less than the first. So, if you wanted to use strcmp, you would do it like this:

    return strcmp(p1.first.c_str(), p2.first.c_str()) < 0;
    

    But I don’t see why you would do that.

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

Sidebar

Related Questions

Help me please. object.cpp: bool OBJECT::operator== (const OBJECT &object) const { return *this ==
First the code: class myClass(object): def __cmp__(self, other): return cmp(type(self), type(other)) or cmp(self.__something, other.__something)
I wrote my comparison function int cmp(const int * a,const int * b) {
I am trying to parse the following using Dom parser in Android. <offerURL> http://statTest.dealtime.com/DealFrame/DealFrame.cmp?bm=553&BEFID=93767&aon=%5E1&MerchantID=434524&crawler_id=1909400&dealId=TCk4NTG97Aa3wSQgh2U3FQ%3D%3D&url=http%3A%2F%2Frover.ebay.com%2Frover%2F1%2F707-64686-24023-0%2F2%3Fipn%3Dpsmain%26icep_item_id%3D190622592957%26icep_vectorid%3D260601%26kwid%3D1%26mtid%3D637%26crlp%3D1_260601%26kw%3D%7Bquery%7D%26query%3D%7Bquery%7D%26linkin_id%3D%7Blinkin_id%7D%26sortbid%3D%7Bbidamount%7D%26fitem%3D190622592957%26mt_id%3D637&linkin_id=7000251&Issdt=120323134700&searchID=p2.77722a731149145f60fa&DealName=Samsung+B2100+Outdoor+In+Schwarz+%28black%29+Orig.+Neuware&dlprc=89.95&crn=&istrsmrc=1&isathrsl=0&AR=1&NG=3&NDP=6&PN=1&ST=7&DB=sdcprod&MT=phx-pkadu-intl-dc20&FPT=DSP&NDS=&NMS=&MRS=&PD=95929320&brnId=14863&IsFtr=0&IsSmart=0&DMT=&op=&CM=&DlLng=7&RR=1&cid=&semid1=&semid2=&IsLps=0&CC=0&SL=0&FS=1&code=&acode=538&category=&HasLink=&frameId=&ND=&MN=&PT=&prjID=&GR=&lnkId=&VK=
I am trying to compare std::string s in a locale-dependent manner. For ordinary C-style
I am using this usort function to sort the array by value: function cmp($a,
The first of the following two functions, fn-apply-cmp-vals, returns a partial function that is
I am trying to do a jump table in C like this cmp eax,
i have a class like this: class test { vector<expr*> _exprs; bool cmp(expr* e1,
class x: def __init__(self,name): self.name=name def __str__(self): return self.name def __cmp__(self,other): print(cmp method called

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.