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

The Archive Base Latest Questions

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

Possible Duplicate: What requirements must std::map key classes meet to be valid keys? I

  • 0

Possible Duplicate:
What requirements must std::map key classes meet to be valid keys?

I want to use std::map as map from my class to another one. If I try the following code, I get an error “undefined operator <“. Does it mean that I need an ordering on class K to use map? And does it have to be full ordering? And do I need all four ordering operators or > is enough?

#include <iostream>
#include <map>
#include <stdio.h>
using namespace std;

struct K {
    int i, j;

    K(int i, int j) : i(i), j(j){}

    friend bool operator==(const K& x, const K& y){ return (x.i==y.i)&&(x.j==y.j); }
    friend bool operator!=(const K& x, const K& y){ return !(x==y); }

/*  friend bool operator<(const K&x, const K&y){
        if(x.i<y.i) return true;
        if(x.i>y.i) return false;
        return x.j<y.j;
    }
    friend bool operator>(const K&x, const K&y){ return y<x; }
    friend bool operator<=(const K&x, const K&y){ return !(y<x); }
    friend bool operator>=(const K&x, const K&y){ return !(x<y); }
*/
};


int main(){
    map<K, float> m;
    m[K(1,2)]=5.4;
    if(m.find(K(1,2))!=m.end())
        cout << "Found: " << m[K(1,2)] << endl;
    else
        cout << "Not found" << endl;
    return 0;
}
  • 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-12T21:54:41+00:00Added an answer on June 12, 2026 at 9:54 pm

    Yes, you need a way to compare elements (operator<) in order to use the std::map. One of the features of map is that it keeps its contents in sorted order, but to achieve this its need to know how to compare items.

    You have three options to implement a comparison method:

    1. Add operator< definition in K
    2. Make a comp functor that know to how to compare two K elements and add this as a template parametermap<K, float, comp> m;

      struct comp {
          bool operator()(const K& first, const K& second) {
              /*****/
          }
      };
      
    3. You can define the std::less specialization for K

      template<>  struct less<K>
      {
          bool operator()(const K& first, const K& second) {
              /*****/
          }
      };
      

    And simple use map<K, float> m;

    This works because by the template definition for map has the compare function set to std::less.

    template < class Key, class T, class Compare = less,
    class Allocator = allocator > > class map

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

Sidebar

Related Questions

Possible Duplicate: How to sort a Map<Key, Value> on the values in Java? I
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How to do the vector of sets in C++? I want to
Possible Duplicate: check whether internet connection is available with C# I just want to
Possible Duplicate: php regex , extract phone number from text/html I have following requirement.
Possible Duplicate: Java Ordered Map I have list of product object in the HashMap<Integer,Product>
Possible Duplicate: From Date will not be accept earlier than 13 months in javascript
Possible Duplicate: Auditing SQL Server data changes My requirements demand for each row inserted
Possible Duplicate: Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
Possible Duplicate: What is the difference between "typename" and "class" template parameters? When defining

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.