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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T10:28:48+00:00 2026-06-06T10:28:48+00:00

I want to define a generic function for printing contents of std::map like types.

  • 0

I want to define a generic function for printing contents of std::map like types. My initial attempt is a function like this:

template <class K, class V>
inline void PrintCollection(const std::map<K,V>& map,
                            const char* separator="\n",
                            const char* arrow="->",
                            const char* optcstr="") {
  typedef typename std::map<K,V>::const_iterator iter_type;
  std::cout << optcstr;
  for (iter_type begin = map.begin(), it = begin, end = map.end();
       it != end; ++it) {
    if (it != begin) {
      std::cout << separator;
    }
    std::cout << it->first << arrow << it->second;
  }
  std::cout << std::endl;
}

which works fine. When I try to generalize this function one more step, i.e. make it work for std::multimap type, compiler becomes angry. I tried several ways to make std::map generic in the function definition, such as:

template <class M, class K, class V>
inline void PrintCollection(const M<K,V>& map,
                            const char* separator="\n",
                            const char* arrow="->",
                            const char* optcstr="") {
  typedef typename M<K,V>::const_iterator iter_type;
  std::cout << optcstr;
  for (iter_type begin = map.begin(), it = begin, end = map.end();
       it != end; ++it) {
    if (it != begin) {
      std::cout << separator;
    }
    std::cout << it->first << arrow << it->second;
  }
  std::cout << std::endl;
}

with no success.

How can I generalize this function as I defined above?

To be more clear, I have already a function defined for vector-like classes defined before this function. It is like

template <class T>
inline void PrintCollection(const T& collection,
                            const char* separator="\n",
                            const char* optcstr="") {
  typedef typename T::const_iterator iter_type;

  std::cout << optcstr;

  for (iter_type begin = collection.begin(), it = begin, end = collection.end();
       it != end;
       ++it) {
    if (it != begin) {
      std::cout << separator;
    }
    std::cout << *it;
  }

  std::cout << std::endl;
}

So what I want to achieve it to make this function specialized to map-like classes. I’m pretty new in C++, so I don’t know the exact term for this kind of stuff. Is this called “template specialization”?

  • 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-06T10:28:49+00:00Added an answer on June 6, 2026 at 10:28 am

    Do it like the stdlib does and use iterators in your algorithm interfaces. This is the most generic solution.

    template<class Iter>
    void PrintCollection(Iter first, Iter last,
                         const char* separator="\n",
                         const char* arrow="->",
                         const char* optcstr="") 
    {
        typedef Iter iter_type;
        std::cout << optcstr;
        for (iter_type begin = first, it = begin, end = last;
            it != end; ++it) {
        if (it != begin) {
            std::cout << separator;
        }
        std::cout << it->first << arrow << it->second;
        }
        std::cout << std::endl;
    }
    
    
    int main()
    {
        vector<pair<int, int>> collection;
        map<int, int> collection2;
        pair<int, int> collection3[3];
    
        PrintCollection(begin(collection), end(collection));
        PrintCollection(begin(collection2), end(collection2));
        PrintCollection(begin(collection3), end(collection3));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to define a generic function that gets the next value in
I have a abstract generic class. I want to define a method inside there
I would like to define a template function but disallow instantiation with a particular
At first sorry my bad English. I want define routing for this url: For
I want to define a following function: if(stmtToFinalize) { NSLog(@Finalizing statement stmtToFinalize); if (sqlite3_finalize(stmtToFinalize)
I want to define some properties on a class using the [Indexable()] attribute in
I want to define a class that supports __getitem__ , but does not allow
I want to define a constant in objective-c. Previously I had the following function:
I want to define a class method that has access to a local variable.
I want create a generic (general) method which accepts the return types of LINQ

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.