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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:41:18+00:00 2026-06-14T07:41:18+00:00

Given an iterator, is it possible to retrieve/use the correct comparison function for the

  • 0

Given an iterator, is it possible to retrieve/use the correct comparison function for the collection that this iterator refers to?

For example, let’s assume I’m writing a generic algorithm:

template <class InIt, class T>
void do_something(InIt b, InIt e, T v) { 
    // ...
}

Now, let’s say I want to do something simple, like find v in [b..e). If b and e are iterators over a std::vector, I can simply use if (*b == v) .... Let’s assume, however, that b and e are iterators over a std::map. In this case, I should only compare the keys, not the whole value type of what’s contained in the map.

So the question is, given those iterators into the map, how do I retrieve that map’s comparison function that will only compare the keys? At the same time, I don’t want to blindly assume that I’m working with a map either. For example, if the iterators pointed to a set, I’d want to use the comparison function defined for that set. If they pointed to a vector or deque, I’d probably have to use ==, because those containers won’t have a comparison function defined.

Oh, almost forgot: I realize that in many cases, a container will only have an equivalent of operator< rather than operator== for the elements it contains — I’m perfectly fine with being able to use that.

  • 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-14T07:41:20+00:00Added an answer on June 14, 2026 at 7:41 am

    There is no standard way to map from an iterator to the underlying container type (if there is such a container at all). You might be able to use some heuristics to try to determine which container, although that will not be simple and probably not guaranteed either.

    For example, you can use a metafunction to determine whether the *value_type* is std::pair<const K, T>, which is a hint that this could be a std::map and after extracting the types K and T try to use a metafunction to determine whether the type of the iterator and the type of std::map<K,T,X,Y>::iterator or std::map<K,T,X,Y>::const_iterator match for a particular combination of X, Y.

    In the case of the map that could be sufficient to determine (i.e. guess with a high chance of success) that the iterator refers to a std::map, but you should note that even if you can use that and even extract the type X of the comparator, that is not sufficient to replicate the comparator in the general case. While uncommon (and not recommended) comparators can have state, and you would not know which is the particular state of the comparator without having access to the container directly. Also note that there are cases where this type of heuristic will not even help, in some implementations of std::vector<> the iterator type is directly a pointer, and in that case you cannot differentiate between an ‘iterator’ into an array and an iterator into a std::vector<> of the same underlying types.

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

Sidebar

Related Questions

Given just a std::string iterator, is it possible to determine the start and end
It is easy given a container to get the associated iterators, example: std::vector<double>::iterator i;
Given this code, is it possible to change dumpStrings() to be able to iterate
This was an interview question: Given an amount, say $167.37 find all the possible
Given such a code segment: #include <iostream> #include <iterator> #include <fstream> #include <string> using
Given that the web application doesn't have su privileges, I'd like to execute a
Given this method to work on a HTML page in a webbrowser: bool semaphoreForDocCompletedEvent;
Given the following code: $(.force-selection).blur(function() { var value = $('matched-item').val(); //check if the input's
Is there any standard implementation (meaning stdlib or boost) of an iterator that wraps
This is my code to construct a possible tour of citys in a Locale

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.