1 //===----------------------------------------------------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is dual licensed under the MIT and the University of Illinois Open 6 // Source Licenses. See LICENSE.TXT for details. 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 20 #include "min_allocator.h" 21 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 } 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 154 } 155