Click here to Skip to main content
15,867,568 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi Team

I have a problem and need help, i have class program that uses data structure and need to implement a test function and be able to print it for values that associate with it by using std::map. Below is my code

What I have tried:

C++
#include <iostream>
#include <map>


class interval_map {
    friend void IntervalMapTest();
    V m_valBegin;
    typedef std::map<K,V> m_map;
    
public:
    // constructor associates whole range of K with val
    interval_map(V const& val)
    : m_valBegin(val)
    {}

    // Assign value val to interval [keyBegin, keyEnd).
    // Overwrite previous values in this interval.
    // Conforming to the C++ Standard Library conventions, the interval
    // includes keyBegin, but excludes keyEnd.
    // If !( keyBegin < keyEnd ), this designates an empty interval,
    // and assign must do nothing.
    void assign( K const& keyBegin, K const& keyEnd, V const& val )
    {
        for( auto it = m_map.find( keyBegin ); * it != keyEnd; ++it )
        {
            * it = val;
        }
    }
    
    // look-up of the value associated with key
    V const& operator[]( K const& key ) const {
        auto it=m_map.upper_bound(key);
        if(it==m_map.begin()) {
            return m_valBegin;
        } else {
            return (--it)->second;
       }
    }
};
Posted
Updated 13-Sep-22 16:35pm
v2
Comments
CPallini 14-Sep-22 1:58am    
Your code has many issues. You should fix it, before proceeding with the test function.

1 solution

I am not sure how this even compiles. The item m_map is declared as a typedef but I see no instance of that type declared.

Anyway, the documentation for map::begin has sample code that displays a map's contents : map::begin - C++ Reference[^].
 
Share this answer
 
Comments
CPallini 14-Sep-22 1:57am    
My 5.
"I am not sure how this even compiles. The item m_map is declared as a typedef but I see no instance of that type declared."
Moreover it is used as instance.
Rick York 14-Sep-22 13:08pm    
Thank you, sir.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900