Click here to Skip to main content
15,882,017 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Team

I need help on my code, i must first find value associate with a key but not to duplicate. I tried on my void assign method to assign a value to it using map.Below is the example of what the program should do in c++ standard library

-2 -> 'A'
-1 -> 'A'
0 -> 'A'
1 -> 'B'
2 -> 'B'
3 -> 'A'
4 -> 'A'
5 -> 'A'


What I have tried:

C++
<pre>#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;
		}
	}
};

int main()
{
    m_map map;
    m_map::iterator mpIter;
    
    int keyEnd, keyBegin;
    int count;
    
    // keep track of values with a std::set
    std::set<std::string> values;

    for(count = 0; count < 3; count++)
    {
        cin >> key;
        cin >> value;

        auto found = map.find(key);

        if(found != map.end()) // key already in map
            continue; // don't add it again

        // now try to add it to the set
        // only add to the map if its value is not already in the set
        if(values.insert(value).second)
            map.insert(std::make_pair(key, value));
    }

    
    return 0;
}
Posted
Updated 9-Sep-22 5:11am
Comments
CPallini 9-Sep-22 7:50am    
Could you please detail your requirements?
It is not clear, for instance, if you can or cannot use the C++ standard libray.
(Actually it is not clear even what are you trying to do).
Richard MacCutchan 9-Sep-22 8:17am    
Why have you created a class that contains a std::map to implement a map? Why not just use std::map in the first place?
Gcobani Mkontwana 9-Sep-22 20:26pm    
@Richard MacCutchan basically i am implementing a logic on void assign method and below or after where the comment is written should look for a value associate with it not to duplicate it and use int main to run or compile it. std::map i must use it in order to achieve it. Perhaps on my code if could be shown where my issue is or if it could be revised it better. I can understand my mistake.

1 solution

You are already doing that in your code. Maybe you should read this : map::find - C++ Reference[^] because that is what you are doing currently and that is how you find values in a map.
 
Share this answer
 
Comments
Gcobani Mkontwana 9-Sep-22 20:27pm    
@Rick York is possible to show me based on what i have written so far, so can understand where my problem was, i would appreciate that a lot and struggling
Rick York 9-Sep-22 21:27pm    
I have no idea where or what your problem is. I am just pointing out that find is the way to find a value associated with a key so read the documentation so that you understand that. You are already calling it twice in your code. Maybe you should consider using a debugger. I find them invaluable.

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