std::map program

please help me to find the problem :
 
// this is my code , i should Assign value val to interval [keyBegin, keyEnd).  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <assert.h>
 #include <map>
 #include <limits>

void assign( K const& keyBegin, K const& keyEnd, const V& val )
	{

		if(!(keyBegin<keyEnd))
		{
			return;
		}
		//std::map<K,V>::iterator it =m_map.begin();

		it=m_map.begin();

	

		for(int j=keyBegin ;j<keyEnd;j++)
		{
			it = m_map.find(j);
			if(it==m_map.end())
			{
				m_map.insert(pair<K,V>(j,val));
			}
			else
			{
				 m_map.erase(it);
				m_map.insert(pair<K,V>(j, val));
			}
		}

	 
	
 };

int main()
{
     interval_mapping<int, char> myMap;
}
Last edited on
That is very vaque.

What is m_map?

Why are you using aMap to hold a lower and upper limit?

What is the class supposed to do?

Exactly ...

m_map is a map object .> see map library

i don't want to use a lower and upper ... i want to insert by a range of map ...

clash should Insert and rewrite if find something equal ...


Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
void assign( K const& keyBegin, K const& keyEnd, const V& val )
{
    // Hint:

    // 1. what would aMap.lower_bound(keyBegin) give you?
    // http://en.cppreference.com/w/cpp/container/map/lower_bound

    // 2. what would aMap.upper_bound(keyEnd) give you?
    // http://en.cppreference.com/w/cpp/container/map/upper_bound

    // ...

}


thanks a lot
one more thing ,
do you know how to test it ?

my mean is i couldn't define (iterator ) outside the Assign routine ...

any help ???
1
2
3
4
5
6
7
8
9
10
11
template < typename K, typename V > struct interval_map
{
    typename std::map<K,V>::const_iterator begin() const { return amap.begin() ; }
    typename std::map<K,V>::const_iterator end() const { return amap.end() ; }
    typename std::map<K,V>::size_type size() const { return amap.size() ; }

    // ...

    private:
        std::map<K,V> amap ;
};
it's ok ,

Upperbond and LowerBond are not begin and end ... they just using for finding in previous ready map.

i have a range and i want to map them ...
insert is ok ...
but at first my iterator is empty ... even after setting it with m_map.begin ()

and m_map.find()


maybe it depends on my loop , isn't it ?
Last edited on
Topic archived. No new replies allowed.