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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:50:42+00:00 2026-06-07T22:50:42+00:00

I wrote a function that finds the most common element in any given container

  • 0

I wrote a function that finds the most common element in any given container (see code below), where the input are two const_iterators for that container. If I call this function however with findMostFrequent(ivec.begin(), ivec.end()) where ivec is a vector<int>, the compiler cannot deduce the template arguments. Calling the function with findMostFrequent< vector<int> >(ivec.begin(), ivec.end()) works fine, but it seems cumbersome. Is there a way to let the compiler find out which template to instantiate?

template <typename T> typename T::value_type findMostFrequent(typename T::const_iterator beg, typename T::const_iterator end)
{
    // T is the type of container, T::value_type is the type which is stored in the container
    typename T::size_type current_streak = 0, max_streak = 0;
    T::value_type max_so_far;
    for (T::const_iterator iter = beg; iter != end; ++iter)
    {
        current_streak = count(beg, end, *iter);
        if ( current_streak > max_streak )
        {
            max_so_far = *iter;
            max_streak = current_streak;
        }
    }
    return max_so_far;
}
  • 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-07T22:50:44+00:00Added an answer on June 7, 2026 at 10:50 pm

    The compiler can not deduce nested specifiers. “Find me a type that has a nested specifier that is equal to the type that I see as a parameter” is too complicated and often ambiguous to resolve it easily. Instead, try to deduce everything you need to know from the iterators directly:

    template <typename IteratorType>
    typename std::iterator_traits<IteratorType>::value_type
    findMostFrequent(IteratorType beg, IteratorType end)
    {
        typename std::iterator_traits<T>::difference_type current_streak = 0, max_streak = 0;
        typename std::iterator_traits<T>::value_type max_so_far;
        for (IteratorType  iter = beg; iter != end; ++iter)
        {
            current_streak = count(beg, end, *iter);
            if ( current_streak > max_streak )
            {
                max_so_far = *iter;
                max_streak = current_streak;
            }
        }
        return max_so_far;
    }
    

    The reason why you should use std::iterator_traits<T>::value_type instead of T::value_type is that it enables you to also have these “iterator specifications” for types that do not naturally define them, such as pointers. For example, T* is actually a valid iterator for this snippet, but it does not contain a value_type typedef/type.

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

Sidebar

Related Questions

So I wrote this function that is given possible numbers, and it has to
I wrote a function that scans a tab delimited file of baseball stats. public
i wrote a custom jquery function that accept a callback but not being able
I wrote a simple function, that makes emacs add matching quotes (so when I
I've got a function I wrote quite some time ago that works fine, but
I have a C++ DLL that I wrote that has a single exposed function,
Given an unsorted array of numbers, write a function that returns true if array
Im using the most recent moo release and trying to write a function that
I'm trying to write a function that does the following: Given a text file,
I've wrote a MIPS subroutine that implements the merge sort algorithm (the code is

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.