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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:38:17+00:00 2026-06-17T21:38:17+00:00

I got a vector containing pairs. My pairs have template parameters. std::vector<std::pair<T1, T2> >

  • 0

I got a vector containing pairs. My pairs have template parameters.

std::vector<std::pair<T1, T2> > myVector;

I would like to sort myVector by the pairs second data tag, so by the “value”(T2), not the “key”(T1).
I saw here that i can use this sweet method:

std::sort(myVector.begin(), myVector.end(), mySortingFunc);

and this is my sortFunc:

bool mySortingFunc (std::pair<T1, T2> pair1,   std::pair<T1, T2> pair2){ 
     return (pair1.second<pair2.second); 
}

and its not compiling, throwing me 10 kilometers long error. (i use g++)
Any advice how should i do this?

  • E D I T:

Actual code:

template<typename T1, typename T2>
class OrderedMMap
{

std::vector<std::pair<T1, T2> > myVector;

public:

bool sortFunc (std::pair<T1, T2> pair1,   std::pair<T1, T2> pair2) { 

    return (pair1.second<pair2.second); 
}

void sortIt()
{
    std::sort(myVector.begin(), myVector.end(), sortFunc);
}
};
  • 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-17T21:38:18+00:00Added an answer on June 17, 2026 at 9:38 pm

    Your sortFunc is a non-static member function. That is the problem. A non-static member function can be invoked only on an object of the class; std::sort cannot do that for you.

    The easy fix is to make the function static:

    static bool sortFunc (std::pair<T1, T2> pair1,   std::pair<T1, T2> pair2) { 
        return (pair1.second<pair2.second); 
    }
    

    With the static keyword, now it became just like regular function, which can be invoked without class instance, which means std::sort will work now.

    It would be good if the function accepts the arguments by const reference:

    static bool sortFunc(std::pair<T1,T2> const& p1,std::pair<T1,T2> const& p2) 
    { 
        return p1.second < p2.second; 
    }
    

    Hope that helps.

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

Sidebar

Related Questions

I've got a multimap<pair<string,string>,vector> > mmap; and I would like to iterate over it
Lets say that I have got a vector like this. std::vector<a_complicated_whatever_identifier *> *something =
I've got a vector<MyType> and would like another vector<MyType> containing only those MyTypes which
I've got raw data streams from image files, like: vector<char> rawData(fileSize); ifstream inFile(image.jpg); inFile.read(&rawData[0]);
I've got a vector of structs in C++ and I would like to modify
I've got the following when building a graph. #include <vector> using namespace std; template<class
I've got a std::vector<Foo> where Foo is a class containing Foo( Foo&& ) noexcept
I've got a std::vector which I need to sort by selected algorithms for certain
I've got a data frame containing a vector of x values, a vector of
For instance, if I have got a vector describing a rectangle xy=[165 88; 401

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.