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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:30:14+00:00 2026-05-23T09:30:14+00:00

I am trying to write a template function that will take an STL container

  • 0

I am trying to write a template function that will take an STL container and will display all the occurrences of the elements in it and the number that they have occurred. I am planning to use a map, iterate through the container and either add a new element if it does not exist or increment the count of occurrences for the element.

Declaration:

template < typename Container_t >
void findOccurrences (const Container_t& inContainer);

My question is: can I somehow get the type specifier of the element that the container holds?
So when I create my map the key value would be the element in the inContainer.
Something like :

map < typeid ( * inContainer.begin()), int > occurrences;

Or would I have to change my template to something like this:

template < typename Container_t , typename Element_t >
void findOccurrences ( const Container_t & inContainer , Element_t dummy )
{
  map < Element_t , int > occurrences;
}

Thanks

  • 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-23T09:30:15+00:00Added an answer on May 23, 2026 at 9:30 am

    How about something like this:

    #include <map>
    #include <iterator>
    
    template <typename Iter>
    void histogram(Iter begin, Iter end)
    {
      typedef typename std::iterator_traits<Iter>::value_type T;
    
      std::map<T, size_t> h;
    
      while (begin != end) ++h[*begin++];
    
      // now h holds the count of each distinct element
    }
    

    Usage:

    std::vector<std::string> v = get_strings();
    histogram(v.begin(), v.end());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write a template function that accepts a std::function which depends
I'm trying to figure out how to write this function: template <typename Bound> Bound::result_type
Trying to write a PowerShell cmdlet that will mute the sound at start, unless
Trying to write a couple of functions that will encrypt or decrypt a file
I'm trying to write a function that can call a webmethod from a webserive
I am trying to write a class template that provides a comparison operator between
I am trying to write a function that can handle both char & wchar_t
I'm trying to write a functor that I can pass to std::sort that will
I am trying to write a translation function that takes the value and the
I'm trying to write a generic filtering function that performs linear interpolation at a

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.