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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T05:01:10+00:00 2026-05-29T05:01:10+00:00

Say I had a template function that calculates the sum of a map’s elements

  • 0

Say I had a template function that calculates the sum of a map’s elements like this:

template <class A, class B> double Total(const map<A, B>& cntr)
{
    map<A, B>::const_iterator iter;
    double total = 0;

    for (iter = cntr.begin(); iter != cntr.end(); ++iter)
        total += iter->second;

    return total;
}

It would work fine whenever B was a numeric type; however, obviously if you passed in a reference to something like map<int, string>, it wouldn’t work.

I was wondering how I could test the type contained in the non-key section of the map so I can through an exception or something. I tried using isdigit() but couldn’t seem to get a working version.

EDIT: My intended use of the test would be to add functionality so that the int type is always used to calculate the total, so I can pass in map<int, string> or map<string, int> and it will always calculate the int types.

  • 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-29T05:01:11+00:00Added an answer on May 29, 2026 at 5:01 am

    You can use type traits in C++11 or boost.

    #include <type_traits>
    
    template <class A, class B> double Sum(const map<A, B>& cntr)
    {
        static_assert(std::is_arithmetic<B>::value, "Invalid type!");
        map<A, B>::const_iterator iter;
        double total = 0;
    
        for (iter = cntr.begin(); iter != cntr.end(); ++iter)
            total += iter->second;
    
        return total;
    }
    

    Keep in mind that types that are invalid will simply not compile, even without the assertion.

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

Sidebar

Related Questions

Let's say I had a program in C# that did something computationally expensive, like
Let's say I had an app that was an address book. I'd like to
say I had a stored proc like this: BEGIN DECLARE user_id INT; SET user_id
Let's say I had the email address like putin-crab@президент.рф How to validate that address
Let's say that I had an array in the Python backend and wanted to
let say i had this two facts. animal(fifi,10). animal(fofo,20). If i call animal(X,Y). then
Say I had class/struct Foo struct Foo { int a, b; bool operator< (Foo
Say I had String s = This is a loooooooooooooooong string; . Now say
Say I had a variable called x and x=5 . I would like to
Say you had this text: SOMETHING_XXXXXXXXXXXXXX_ELSE SOMETHING_XXXXXXXXXXXXXX_ELSE2 SOMETHING_XXXXXXXXXXXXXX_ELSE3 SOMETHING_XXXXXXXXXXXXXX_ELSE4 And you wanted to replace

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.