Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===----------------------------------------------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | // <map> |
| 11 | |
| 12 | // class map |
| 13 | |
| 14 | // mapped_type& at(const key_type& k); |
| 15 | // const mapped_type& at(const key_type& k) const; |
| 16 | |
| 17 | #include <map> |
| 18 | #include <cassert> |
| 19 | |
Marshall Clow | 061d0cc | 2013-11-26 20:58:02 +0000 | [diff] [blame] | 20 | #include "min_allocator.h" |
Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 +0000 | [diff] [blame] | 21 | |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 22 | int main() |
| 23 | { |
| 24 | { |
| 25 | typedef std::pair<const int, double> V; |
| 26 | V ar[] = |
| 27 | { |
| 28 | V(1, 1.5), |
| 29 | V(2, 2.5), |
| 30 | V(3, 3.5), |
| 31 | V(4, 4.5), |
| 32 | V(5, 5.5), |
| 33 | V(7, 7.5), |
| 34 | V(8, 8.5), |
| 35 | }; |
| 36 | std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
| 37 | assert(m.size() == 7); |
| 38 | assert(m.at(1) == 1.5); |
| 39 | m.at(1) = -1.5; |
| 40 | assert(m.at(1) == -1.5); |
| 41 | assert(m.at(2) == 2.5); |
| 42 | assert(m.at(3) == 3.5); |
| 43 | assert(m.at(4) == 4.5); |
| 44 | assert(m.at(5) == 5.5); |
| 45 | try |
| 46 | { |
| 47 | m.at(6); |
| 48 | assert(false); |
| 49 | } |
| 50 | catch (std::out_of_range&) |
| 51 | { |
| 52 | } |
| 53 | assert(m.at(7) == 7.5); |
| 54 | assert(m.at(8) == 8.5); |
| 55 | assert(m.size() == 7); |
| 56 | } |
| 57 | { |
| 58 | typedef std::pair<const int, double> V; |
| 59 | V ar[] = |
| 60 | { |
| 61 | V(1, 1.5), |
| 62 | V(2, 2.5), |
| 63 | V(3, 3.5), |
| 64 | V(4, 4.5), |
| 65 | V(5, 5.5), |
| 66 | V(7, 7.5), |
| 67 | V(8, 8.5), |
| 68 | }; |
| 69 | const std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
| 70 | assert(m.size() == 7); |
| 71 | assert(m.at(1) == 1.5); |
| 72 | assert(m.at(2) == 2.5); |
| 73 | assert(m.at(3) == 3.5); |
| 74 | assert(m.at(4) == 4.5); |
| 75 | assert(m.at(5) == 5.5); |
| 76 | try |
| 77 | { |
| 78 | m.at(6); |
| 79 | assert(false); |
| 80 | } |
| 81 | catch (std::out_of_range&) |
| 82 | { |
| 83 | } |
| 84 | assert(m.at(7) == 7.5); |
| 85 | assert(m.at(8) == 8.5); |
| 86 | assert(m.size() == 7); |
| 87 | } |
Howard Hinnant | 70342b9 | 2013-06-19 21:29:40 +0000 | [diff] [blame] | 88 | #if __cplusplus >= 201103L |
| 89 | { |
| 90 | typedef std::pair<const int, double> V; |
| 91 | V ar[] = |
| 92 | { |
| 93 | V(1, 1.5), |
| 94 | V(2, 2.5), |
| 95 | V(3, 3.5), |
| 96 | V(4, 4.5), |
| 97 | V(5, 5.5), |
| 98 | V(7, 7.5), |
| 99 | V(8, 8.5), |
| 100 | }; |
| 101 | std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
| 102 | assert(m.size() == 7); |
| 103 | assert(m.at(1) == 1.5); |
| 104 | m.at(1) = -1.5; |
| 105 | assert(m.at(1) == -1.5); |
| 106 | assert(m.at(2) == 2.5); |
| 107 | assert(m.at(3) == 3.5); |
| 108 | assert(m.at(4) == 4.5); |
| 109 | assert(m.at(5) == 5.5); |
| 110 | try |
| 111 | { |
| 112 | m.at(6); |
| 113 | assert(false); |
| 114 | } |
| 115 | catch (std::out_of_range&) |
| 116 | { |
| 117 | } |
| 118 | assert(m.at(7) == 7.5); |
| 119 | assert(m.at(8) == 8.5); |
| 120 | assert(m.size() == 7); |
| 121 | } |
| 122 | { |
| 123 | typedef std::pair<const int, double> V; |
| 124 | V ar[] = |
| 125 | { |
| 126 | V(1, 1.5), |
| 127 | V(2, 2.5), |
| 128 | V(3, 3.5), |
| 129 | V(4, 4.5), |
| 130 | V(5, 5.5), |
| 131 | V(7, 7.5), |
| 132 | V(8, 8.5), |
| 133 | }; |
| 134 | const std::map<int, double, std::less<int>, min_allocator<V>> m(ar, ar+sizeof(ar)/sizeof(ar[0])); |
| 135 | assert(m.size() == 7); |
| 136 | assert(m.at(1) == 1.5); |
| 137 | assert(m.at(2) == 2.5); |
| 138 | assert(m.at(3) == 3.5); |
| 139 | assert(m.at(4) == 4.5); |
| 140 | assert(m.at(5) == 5.5); |
| 141 | try |
| 142 | { |
| 143 | m.at(6); |
| 144 | assert(false); |
| 145 | } |
| 146 | catch (std::out_of_range&) |
| 147 | { |
| 148 | } |
| 149 | assert(m.at(7) == 7.5); |
| 150 | assert(m.at(8) == 8.5); |
| 151 | assert(m.size() == 7); |
| 152 | } |
| 153 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 154 | } |