13 template <
class Key,
class T,
class IgnoredLess = std::less<Key>,
14 class Allocator = std::allocator<std::pair<const Key, T>>>
15 struct ordered_map : std::vector<std::pair<const Key, T>, Allocator>
18 using mapped_type = T;
19 using Container = std::vector<std::pair<const Key, T>, Allocator>;
20 using typename Container::iterator;
21 using typename Container::const_iterator;
22 using typename Container::size_type;
23 using typename Container::value_type;
27 ordered_map(
const Allocator& alloc = Allocator()) : Container{alloc} {}
29 ordered_map(It first, It last,
const Allocator& alloc = Allocator())
30 : Container{first, last, alloc} {}
31 ordered_map(std::initializer_list<T> init,
const Allocator& alloc = Allocator() )
32 : Container{init, alloc} {}
34 std::pair<iterator, bool> emplace(
const key_type& key, T&& t)
36 for (
auto it = this->begin(); it != this->end(); ++it)
43 Container::emplace_back(key, t);
44 return {--this->end(),
true};
47 T& operator[](
const Key& key)
49 return emplace(key, T{}).first->second;
52 const T& operator[](
const Key& key)
const
59 for (
auto it = this->begin(); it != this->end(); ++it)
67 throw std::out_of_range(
"key not found");
70 const T& at(
const Key& key)
const
72 for (
auto it = this->begin(); it != this->end(); ++it)
80 throw std::out_of_range(
"key not found");
83 size_type erase(
const Key& key)
85 for (
auto it = this->begin(); it != this->end(); ++it)
90 for (
auto next = it; ++next != this->end(); ++it)
93 new (&*it) value_type{std::move(*next)};
95 Container::pop_back();
102 iterator erase(iterator pos)
107 for (
auto next = it; ++next != this->end(); ++it)
110 new (&*it) value_type{std::move(*next)};
112 Container::pop_back();
116 size_type count(
const Key& key)
const
118 for (
auto it = this->begin(); it != this->end(); ++it)
120 if (it->first == key)
128 iterator find(
const Key& key)
130 for (
auto it = this->begin(); it != this->end(); ++it)
132 if (it->first == key)
137 return Container::end();
140 const_iterator find(
const Key& key)
const
142 for (
auto it = this->begin(); it != this->end(); ++it)
144 if (it->first == key)
149 return Container::end();
152 std::pair<iterator, bool> insert( value_type&& value )
154 return emplace(value.first, std::move(value.second));
157 std::pair<iterator, bool> insert(
const value_type& value )
159 for (
auto it = this->begin(); it != this->end(); ++it)
161 if (it->first == value.first)
166 Container::push_back(value);
167 return {--this->end(),
true};
namespace for Niels Lohmann
Definition: adl_serializer.hpp:9
ordered_map: a minimal map-like container that preserves insertion order for use within nlohmann::bas...
Definition: ordered_map.hpp:16