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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:05:38+00:00 2026-05-27T10:05:38+00:00

I add the constructor and two functions to the class of my previous linked

  • 0

I add the constructor and two functions to the class of my previous linked question C++ iterate through a template Map and I need help at this points:

  • What do you reckon this constructor does?
  • Adding one value at the beginning of map?
  • I see though in the respective key only an address as value after initializing in main. What is wrong?

The operator [] is supposed to get the values for a specific key. However I cannot use it so as to get the elements of the map in the output. Any hint?

template<class K, class V>
class template_map{
public:
    template_map( V const& val) {
        m_map.insert(my_map.begin(),std::make_pair(std::numeric_limits<K>::min(),val));
    };    

    typedef typename std::map<K,V> TMap;

    TMap my_map;
    typedef typename TMap::const_iterator const_iterator;
    const_iterator begin() const { return my_map.begin(); }
    const_iterator end() const   { return my_map.end(); }
    V const& operator[]( K const& key ) const {
        return ( --my_map.upper_bound(key) )->second;
    }

    ...
};

int main()
{
    interval_map<int,int> Map1 (10);
    //Show the elements of the map?
}

Consider also that it should be a function that inserts values to the map.

  • 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-27T10:05:38+00:00Added an answer on May 27, 2026 at 10:05 am

    What do you reckon this constructor does? Adding one value at the beginning of map?

    It initialises the map so that map[x] == v for any x. The map associates intervals with values, internally storing a normal map keyed by the start of each interval; it’s initialised so that the entire range of the key type maps to the initial value.

    I see though in the respective key only an address as value after initializing in main. What is wrong? The operator [] is supposed to get the values for a specific key. However I cannot use it so as to get the elements of the map in the output. Any hint?

    I’ve no idea what you’re asking there. If you try, for example, cout << Map1[42] << '\n';, then your program should output 10, since that is the initial value assigned to the entire range of integers.

    Consider also that it should be a function that inserts values to the map.

    Since the internal map is publicly exposed, you can add a new interval to the map with

    Map1.my_map.insert(std::make_pair(interval_start, value));
    

    It might be more polite to make my_map private, and provide an insert() function to do that. You could also add a non-const overload of operator[] that inserts a new range and returns a reference to its value, something like

    V & operator[](K const & key) {
        V const & old_value = (--my_map.upper_bound(key))->second;
        return *my_map.insert(std::make_pair(key, old_value)).first;
    }
    

    although this might not be a great idea, as you’d have to be careful that you don’t accidentally insert many ranges when you only want to read the values.

    My problem is how to iterate through the map to get all its elements and print them in main. It shows me an address with a value of the object initialization.

    Remembering that an iterator over a map refers to a key/value pair (of type std::pair<K,V>), you should be able to iterator over the map like this:

    for (auto it = Map1.begin(); it != Map1.end(); ++it) {
        std::cout << it->first << " maps to " << it->second << '\n';
    }
    

    (in C++03, you’ll need to write template_map<int,int>::const_iterator rather than auto).

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

Sidebar

Related Questions

I'm trying to make a simple Vector class (math) this way: template <int D,
I have two objects defined something like this (simplified for sake of the question):
I need help with creating Operator-Overloaded functions please. I tried 2 but I am
To add a whitespace seperator in a string we use String.Join(). My question:What(and how)
I add my page top link button like this: <a name=top></a> . . .
I want to add two jPanels to a JFrame side by side. the two
How it started? I wanted to add two columns, that are not in business
I need a collection in which i can store heap-allocated objects having virtual functions.
Currently, I have this method to compare two numbers Private Function ETForGreaterThan(ByVal query As
For example, I have a class that has two strings, one of which must

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.