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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:22:23+00:00 2026-06-14T18:22:23+00:00

How to use std::less in a vector that contains classes, all google results are

  • 0

How to use std::less in a vector that contains classes, all google results are with plain int examples.

When it comes to classes, for example:

class A{
   public:
      A( int value = 0 ):m_value(value){};
      int m_value;
};

How to do something like:

std::count_if( m_cells.begin(), m_cells.end(), std::less< int >() );

Less will receive an A, not an int. std::less< A >

Seems like less needs a functor operator()(), how to avoid this? I need to implement operator<( int a ) ? Make a bind? What else?

Code:

  1 #include <iostream>
  2 #include <vector>
  3 #include <algorithm>
  4
  5 using namespace std;
  6
  7 class A
  8 {
  9    public:
 10       A(int a = 0 ):m_value(a) {}
 11       bool operator!=( int a )
 12       {
 13          return m_value != a;
 14       }
 15
 16       bool operator<( A &a )
 17       {
 18          return m_value < a.m_value;
 19       }
 20
 21       int m_value;
 22 };
 23
 24
 25 int main(){
 26    std::vector< A > m_cells( 5 );
 27
 28    m_cells[2].m_value = 3;
 29    m_cells[3].m_value = 4;
 30    m_cells[4].m_value = 4;
 31    std::count_if( m_cells.begin(), m_cells.end(), std::less< A >() );
 32    return 0;
 33 }
 34

This results in:

/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_algo.h:4437: error: no match for call to ‘(std::less<A>) (A&)’
/usr/lib/gcc/x86_64-redhat-linux/4.4.6/../../../../include/c++/4.4.6/bits/stl_function.h:229: note: candidates are: bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = A]
  • 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-14T18:22:24+00:00Added an answer on June 14, 2026 at 6:22 pm

    Implement an operator< for A, and then pass in std::less<A>, other options (for example you could implement a conversion operator to int – YUCKYUCKYUCKYUCK)

    EDIT: To continue…

    Like James says, std::less<> requires two arguments, if you are stuck on c++03 (and don’t have boost), you can do something like the following:

    std::count_if( m_cells.begin(), m_cells.end() ,std::bind1st(std::less<A>(), 3) )
    

    Basically one of the arguments is bound to 3 – this the first argument (consider the left hand side), you can also bind to the second argument (right hand side – using std::bind2nd), depending on how you want the predicate to work.

    This approach requires that you properly implement operator<, i.e.

    bool operator<(A const& a) const
    {
    }
    

    EDIT2: for c++11, you can try any of the following:

     std::cout << std::count_if( m_cells.begin(), m_cells.end() , [](A const& a) { return 3 < a.m_value; } ) << std::endl; // lambda
     std::cout << std::count_if( m_cells.begin(), m_cells.end() , std::bind(std::less<A>(), _1, 4) ) << std::endl; // bind second argument (rhs)
     std::cout << std::count_if( m_cells.begin(), m_cells.end() , std::bind(std::less<A>(), 3, _1) ) << std::endl; // bind first argument (lhs)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I will use std::map<int, A> A is a class and I have to prevent
In attempting to use std::select1st from <functional> in a VS2008 project I found that
One should always use std::string over c-style strings( char * ) is advice that
I recently changed some code to use a set instead of a vector: std::set<b2Body
Originally I had a member that was a std::vector<Point> with a method that did
If I have a vector of pairs: std::vector<std::pair<int, int> > vec; Is there and
I use std::tr1::shared_ptr extensively throughout my application. This includes passing objects in as function
I'm trying to use std::ostringstream to convert a number into a string (char *),
When should I use std::string and when should I use char* to manage arrays
I'm trying to use std::string instead of char* whenever possible, but I worry 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.