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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:16:04+00:00 2026-05-17T02:16:04+00:00

I have an object: map<A*, string> collection; I would like to call the map::find

  • 0

I have an object:

map<A*, string> collection;

I would like to call the map::find function, but the value I have for the key is const, like in the following code, which does not compile:

const A* a = whatever();
collection.find(a);

The following code works and performs the equivalent of the find operation:

const A* a = whatever();
map<A*, string>::iterator iter;
for(iter = collection.begin(); iter != collection.end(); ++iter)
    if(iter->first == a)
        break;
// iter now contains the result or map::end (just like map::find)

But it is probably not as efficient as the find member function, and it’s also ugly and masks the intent of the code.

How can I call the find function?

Thanks

Edit:

I am intentionally using a pointer type for the key in the map. The behaviour I want is for the map to use pointer equality for the keys. (Just like in my loop 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-17T02:16:04+00:00Added an answer on May 17, 2026 at 2:16 am

    Comparing pointers has nothing to do with it. The OP may or may not need a custom compare operation; it looks to me like they’re just looking for a specific object by its address, which seems perfectly reasonable. The first two answers seem to have missed the point that find() doesn’t compile while a handwritten search works.

    The find() call won’t compile because you’re passing it the wrong type to search for. map::find() expects its argument to be the same type as the map’s key type, which is A*. You’re passing a const A*, which is not implicitly convertible to an A* (but is comparable to an A*, which is why the handwritten search works). The implicit conversion only works in the other direction (A* to const A*, not const A* to A*).

    Possibly you should be using const A* instead of A* as the map key; depending on what you’re using the map for, this may or may not be practical. If you need the map key to be A*, you need to pass an A* to find(), which means either starting with an A* as your search target in the first place, or if you can only get it from its original source as a const A*, using find(const_cast<A*>(a)) to turn it into a pointer to non-const (which is not the same as a non-const pointer, a point that confuses a lot of C/C++ coders). Usually const_cast is inadvisable, but here it’s safe since you’re only comparing the pointer, not dereferencing it.

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

Sidebar

Related Questions

I Have a object which the following field : boost::unordered_map<std::string, std::shared_ptr<Foo> > m_liste_; I
I have a std::map<std::string, HANDLE> SampleMap which stores HANDLE objects as value. After the
I have the following code: Map<String, ObjectType> objectMap = new HashMap<String, ObjectType>(); for (ObjectType
I've been trying to map an object to a database using SQLAlchemy but have
I have the following code object DispatchLibrary { private var nodes = Map.empty[java.util.UUID, List[BigInt]]
What kind of collection method would store a pair (key and value) where the
I am looking for a Map-like collection that would allow me to create new
(Redefinition of the method public Collection<String> values(); ) I have a Map contained in
I have a spring-hibernate application which is failing to map an object properly: basically
Hey guys I have a object sitting in my tile map for spawn point

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.