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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:20:55+00:00 2026-05-27T04:20:55+00:00

I have a map with a struct as key, I have overloaded the <

  • 0

I have a map with a struct as key, I have overloaded the < operator, but the map is storing each entry as separate keys even though they are same. The code is as follows:

#include <iostream>
#include <vector>
#include <map>

using namespace std;

struct vertex 
{
    int color;
    vertex *pi;
    int index;

    bool operator<(const vertex & v ) const {
        return this->index < v.index;
    }
    bool operator==(const vertex & v) const {
        return this->index == v.index;
    }
};

int main()
{
    int x, y, num_edges;
    vector<vertex* > v;
    vertex *temp1, *temp2, *temp;
    map<vertex*, vector<vertex*> > m;
    map<vertex*, vector<vertex*> >::iterator it;

    cout << "\nEnter no. of edges: ";
    cin >> num_edges;

    for( int i = 0; i < num_edges; i++ )
    {
        cout << "\nEnter source: ";
        cin >> x;
        cout << "\nEnter dest: ";
        cin >> y;

        temp1 = new vertex;
        temp2 = new vertex;
        temp1->index = x;
        temp2->index = y;

        m[temp1].push_back(temp2);
        m[temp2].push_back(temp1);
    }
    
    temp1 = new vertex;
    temp2 = new vertex;

    cout << "\nPrinting map: " << endl;
    for( it = m.begin(); it != m.end(); it++ )
    {
        temp = (*it).first;

        cout << temp->index << "\t";

        v = (*it).second;
        for( int i = 0; i < v.size(); i++ )
        {
            temp1 = v[i];
            cout << temp1->index << "\t";
        }
        cout << "\n";
        v.clear();
    }
    for( it = m.begin(); it != m.end(); it++ )
    {
        temp = (*it).first;
        v.push_back(temp);
    }
    return 0;
}

The output that I am getting now is:

Enter no. of edges: 4

Enter source: 1

Enter dest: 3

Enter source: 4

Enter dest: 3

Enter source: 4

Enter dest: 2

Enter source: 2

Enter dest: 1

Printing map: 
1   3   
3   1   
4   3   
3   4   
4   2   
2   4   
2   1   
1   2   

But it should be:

1 3 2  
2 4 1      
3 1 4  
4 3 2

Where am I going wrong?

  • 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-05-27T04:20:55+00:00Added an answer on May 27, 2026 at 4:20 am

    The std::map will compare the type you give it as key (vertex*), but you define the < operator on vertex.

    You can use the struct themselves as keys, or -if you have to use pointers- you have to give the map a way to compare the pointers.

    Now, std::map use the std::less as a comparison predicate, that is defined in therm of < (that’s why using the struct themselves, you can achieve the result by overloading <).

    You can either:

    o) define yourself a predicate that compares vertex*: it can be

    template <class T> //assume T is a pointer or pointer-like class
    struct redirected_less : public std::binary_function <T,T,bool> 
    {
        bool operator() (const T& x, const T& y) const {return *x < *y;}
    };
    

    and then define you map as

    std::map<vertex*, vector<vertex*>,  redirected_less<vertex*> >
    

    o) specialize the std::less for vertex* as

    namespace std
    {
         template <> 
         struct less<vertex*> : binary_function <vertex*,vertex*,bool> 
         {
             bool operator() (vertex* x, vertex* y) const {return *x < *y; }
         };
    }
    

    and declare your map as

    std::map<vertex*, vector<vertex*> >
    

    as usual.

    I personally prefer the first (gives a more localized code, less “arcane” in future readings)

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

Sidebar

Related Questions

I have a stl::map which key type is a custom struct. I want to
I have this struct: struct Map { public int Size; public Map ( int
I have unsorted map of key value pairs. input = { xa => xavalue,
I have a problem putting data into an unordered_map using a struct as key:
Extends . I have: struct Coord { int row, col ; bool operator<( const
I have a file context.ml where a map is defined module CtxMap = Map.make(struct
I have 2 map one of them contain data for example struct DATA {
What I would like to do is have a map that maps integer keys
I do not have access to BOOST or STL; my struct and map looks
I have a function that returns a struct that has two fields, :key :event.

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.