Guest User

Untitled

a guest
Mar 24th, 2018
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. Example: the std::map (0,'A'), (3,'B'), (5,'A') represents the mapping
  2.  
  3. 0 -> 'A'
  4. 1 -> 'A'
  5. 2 -> 'A'
  6. 3 -> 'B'
  7. 4 -> 'B'
  8. 5 -> 'A'
  9. 6 -> 'A'
  10. 7 -> 'A'
  11. ... all the way to numeric_limits<key>::max()
  12.  
  13. #include <assert.h>
  14. #include <map>
  15. #include <limits>
  16. #include <iostream> // I added this was not given am I doing right ?
  17.  
  18. using namespace std;
  19.  
  20. template<class K, class V>
  21. class interval_map {
  22. friend void IntervalMapTest();
  23.  
  24. private:
  25. std::map<K,V> m_map;
  26.  
  27. public:
  28. // constructor associates whole range of K with val by inserting (K_min, val)
  29. // into the map
  30. interval_map( V const& val) {
  31. m_map.insert(m_map.begin(),std::make_pair(std::numeric_limits<K>::lowest(),val));
  32. }
  33.  
  34. // Assign value val to interval [keyBegin, keyEnd).
  35. // Overwrite previous values in this interval.
  36. // Do not change values outside this interval.
  37. // Conforming to the C++ Standard Library conventions, the interval
  38. // includes keyBegin, but excludes keyEnd.
  39. // If !( keyBegin < keyEnd ), this designates an empty interval,
  40. // and assign must do nothing.
  41.  
  42.  
  43.  
  44. void assign( K const& keyBegin, K const& keyEnd, V const& val ) {
  45.  
  46. // My code strats
  47.  
  48. if(!(keyBegin < keyEnd))
  49. return;
  50.  
  51. //int it;
  52.  
  53. //std::map<std::string, int>::iterator it = m_map.insert();
  54.  
  55. typename std::map<K,V>::iterator it =m_map.begin();
  56.  
  57.  
  58. for(int j=keyBegin; j<keyEnd; j++)
  59. {
  60. it = m_map.find(j);
  61.  
  62. if(it==m_map.end())
  63. {
  64. m_map.insert(pair<K,V>(j,val));
  65. }
  66. else
  67. {
  68. m_map.erase(it);
  69. m_map.insert(pair<K,V>(j, val));
  70. }
  71. }
  72. //end of my code
  73.  
  74. }
  75.  
  76.  
  77.  
  78. // look-up of the value associated with key
  79. V const& operator[]( K const& key ) const {
  80. return ( --m_map.upper_bound(key) )->second;
  81. }
  82.  
  83.  
  84. // my code again
  85. void IntervalMapTest()
  86. {
  87.  
  88. std::map<char,unsigned int> m_map;
  89. char c;
  90.  
  91. m_map ['A']=0;
  92. m_map ['A']=1;
  93. m_map ['A']=2;
  94.  
  95. for (c='A'; c<'B'; c++)
  96. {
  97. std::cout << c;
  98. if (m_map.count(c)>0)
  99. std::cout << " is an element of mymap.n";
  100. else
  101. std::cout << " is not an element of mymap.n";
  102. }
  103.  
  104. }
  105. //me code ends
  106.  
  107.  
  108.  
  109. };
  110.  
  111. // Many solutions we receive are incorrect. Consider using a randomized test
  112. // to discover the cases that your implementation does not handle correctly.
  113. // We recommend to implement a function IntervalMapTest() here that tests the
  114. // functionality of the interval_map, for example using a map of unsigned int
  115. // intervals to char.
  116.  
  117. int main() {
  118.  
  119. // given IntervalMapTest();
  120.  
  121. // What I did starts
  122.  
  123. interval_map <char, unsigned int> test1 ('K');
  124.  
  125. // instantiation with float and char type
  126.  
  127. interval_map <unsigned int, char> test2 (5);
  128.  
  129. test1.IntervalMapTest();
  130.  
  131. test2.IntervalMapTest();
  132.  
  133. // What I did ends
  134.  
  135. }
Add Comment
Please, Sign In to add comment