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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:08:07+00:00 2026-06-03T22:08:07+00:00

I would like to use algorithm std::include to work with heterogeneous STL collections. In

  • 0

I would like to use algorithm std::include to work with heterogeneous STL collections. In my example I would like to check if a std::vector of integer is included in a std::map.

I would like to solve this problem by using a simple templated function; the reason for this is because I would like to use the C++ template argument deduction to let deduce when first argument of the comparer function is a std::pair vs an int and the other way around ( std::include behind the scenes calls Comp(a,b) and Comp(b,a) ).

Below my code I would like to run

    typedef std::map<int,std::string> dict;
    typedef std::vector<int> vect;

    int data[]={1,2,3,4};
    vect l(data,data+4);
    dict h;
    h.insert(dict::value_type(0,"ciccio"));
    h.insert(dict::value_type(1,"ciccio"));                  
    std::includes( h.begin(),h.end()
        , l.begin(), l.end(), is_my_less );

I tried the following below but it doesn’t compile and say partial specialization is not allowed but also unresolved overloaded function type that makes me think that I am doing something wrong with my function.
Do you know is that is possible by using strictly a templated function?

    template<class T1,class T2,class T3>
    bool is_less_than_pair( const T1&a ,const T2& b ){
        return false;
    };

    template<class T1,class T>
    bool is_less_than_pair<
        T1
        , std::pair<T1,T>
        , T >( const T1&a, const std::pair<T1,T>& b ){
        return a<b.first;
    }

    template<class T1, class T>
    bool is_less_than_pair< 
        std::pair<T1,T>
        ,T1
        , T >( const std::pair<T1,T>& a, const T1& b ){
        return a.first<b;
    }

Now based on the fact that function templates cannot be partially specialized I tried function overloading like below but it didn’t work out and gcc tells me unresolved overloaded function type again. What is the best I can do?

      template<class T1,class T2>
      bool is_less_than_pair( const std::pair<T1,T2>& a ,const T1& b ){
        return a.first<b;
      };
      template<class T1,class T2>
      bool is_less_than_pair( const T1& a ,const std::pair<T1,T2>& b ){
        return b.first<a;
      };
  • 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-03T22:08:08+00:00Added an answer on June 3, 2026 at 10:08 pm

    If you’re restricted to functions, no. You can’t use an overloaded function, because it won’t be able to select the proper overload when passing it to the STL algorithm. And you can’t partially specialize a function template.

    It can be done with a function object, by providing all of the overloads. In effect, you’ll pass all of the overloads to the STL algorithm, and then it will choose the appropriate overload as it’s called. Note, due to the fact that the map’s values are std::pair<const int, string>&, use of the same T1 in both sides of the mixed-mode operators will not work, since the vector’s iterators are turning int&, where the map’s are using a constant.

    struct my_less_than
    {
       template <typename T1>
       bool operator()(const T1& lhs, const T1& rhs) const
       {
          return lhs < rhs;
       }
    
       template <typename T1, typename T2, typename T3>
       bool operator()(const T1& lhs, const std::pair<T2, T3>& rhs) const
       {
          return lhs < rhs.first;
       }
    
       template <typename T1, typename T2, typename T3 >
       bool operator()(const std::pair<T1, T2>& lhs, const T3& rhs) const
       {
          return lhs.first < rhs;
       }
    
       template <typename T1, typename T2, typename T3>
       bool operator()(const std::pair<T1, T2>& lhs, const std::pair<T1, T3>& rhs) const
       {
          return lhs.first < rhs.first;
       }
    };
    

    Usage:

       std::includes( h.begin(),h.end()
            , l.begin(), l.end(), my_less_than() );
    

    Edit: Example http://ideone.com/JFIoy

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

Sidebar

Related Questions

What is the algorithm. I would like to use the same algorithm to manually
I would like to write a dictionary. What algorithms/structures should I use? Each word
I would like to use the group_by method, but instead of using a column
I would like to use complex numbers as defined in C99, but I need
I would like to use HTML 4.01 Strict, and used a DOCTYPE of it
I would like to use KVO in the following context: 1) In touchesBegan:withEvent: I
I would like to use python to make system calls to programs and time
I would like to use a COM object in my application. How can I
I would like to use Runtime.exec() to initiate another process in a directory with
I would like to use Cocos2d on the iPhone to draw a 2D car

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.