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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:51:27+00:00 2026-05-27T01:51:27+00:00

I have a stl::map which key type is a custom struct. I want to

  • 0

I have a stl::map which key type is a custom struct. I want to know if this map already has a key with a specific string as component (noted as “id” below), whatever the value of its other components. Inspired by this answer and this one also, I try to use stl::find_if with a custom functor:

map<myStruct, vector<size_t> > myMap;

struct myStruct
{
  string a, b, c, id;
};

struct checkId : unary_function<pair<myStruct, vector<size_t> >, bool>
{
private:
  string _exp;
public:
  checkId (myStruct x) : _exp(x.id) {}
  bool operator() (const pair<myStruct, vector<size_t> > & p) const
  {
    return p.first.id.compare(_exp) == 0;
  }
};

map<myStruct, vector<size_t> >::iterator it;
myStruct newS;  // to be initialized, but not shown here
it_mP2P = find_if(myMap.begin(), myMap.end(), checkId(newS));

When I compile this, gcc returns me:

/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = myStruct]’:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_map.h:347:    instantiated from ‘_Tp& std::map<_Key,_Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = myStruct, _Tp = std::vector<long unsigned int, std::allocator<long unsigned int> >, _Compare = std::less<myStruct>, _Alloc = std::allocator<std::pair<const myStruct, std::vector<long unsigned int, std::allocator<long unsigned int> > > >]’
myprogram.cpp:386:    instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:227:  error: no match for ‘operator<’ in ‘__x < __y’

Does this mean that I have to overload the operator “<” to work with my custom struct if I want to use my functor “checkId”? How can I do this? I am not a C++ expert,so thanks in advance for any piece of code.

  • 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-27T01:51:28+00:00Added an answer on May 27, 2026 at 1:51 am

    http://codepad.org/VYNqdZeF

    struct myStruct
    {
      std::string a, b, c, id;
      bool operator<(const myStruct& rhs) const //HERES THE MAGIC
      {return id < rhs.id;}                     //WHEEEEEEEEEEEE!
    };
    

    This is required to make std::map<myStruct, stuff> work, without passing a custom comparison functor.

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

Sidebar

Related Questions

If i have a stl map from string to int and i want to
Map type from STL have next type: std::map< Key, Data, Compare, Alloc > As
Suppose I have a STL map where the values are pointers, and I want
If you have an STL vector which has been resized, is it safe to
In my current C++-project I have an STL map which maps integer keys onto
I know that the Map container in STL is internally a Red-Black Tree, which
I have some opaque bytes which I want to use in an std::map ,
I have a custom datastructure, which can be accessed in multiple ways. I want
Suppose I have: stl::map<std::string, Foo> myMap; is the following function thread safe? myMap[xyz] ?
i have to use stl map containing 3 values : float(key) , int ,int.

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.