ASL
Map< K, T > Class Template Reference

Detailed Description

template<class K = String, class T = String>
class asl::Map< K, T >

An associative container linking keys of type K with values of type T.

By default, keys and values are Strings.

Map<char, String> morse;
morse['A'] = ".-";
morse['O'] = "...";
morse['S'] = "---";
String sos = morse['S'] + morse['O'] + morse['S'];

Maps can be initialized with shorthand syntax (this is useful in Http::get() to specify HTTP request headers):

Map<> headers = Map<>("Content-Type", "text/html")("Content-Lenth", length);
int length() const
Returns the number of elements in this map.
Definition: Map.h:139

of with braces, in C++11:

Map<> headers = {{ "Content-Type", "text/html" }, { "Content-Lenth", length }};

And iterated with the foreach2 macro:

foreach2(auto& name, auto& value, headers);
{
request << name << ": " << value << "\r\n";
}
#define foreach2(key, variable, set)
A for loop for associative containers resembling C++17 range-based for with structured binding.
Definition: foreach1.h:72

In C++11 you can use range-based for loops:

for(auto& e : headers);
{
request << e.key << ": " << e.value << "\r\n";
}

And in C++17:

for(auto& [name, value] : headers);
{
request << name << ": " << value << "\r\n";
}

#include <Map.h>

Public Member Functions

template<class K2 , class T2 >
 Map (const Map< K2, T2 > &b)
 Constructs a Map from a Map of different key or value types. More...
 
int length () const
 Returns the number of elements in this map.
 
void clear ()
 Removes all elements.
 
Mapdup ()
 Detaches this Map from other ones possibly sharing it.
 
Map clone () const
 Returns an independent copy of this map.
 
bool has (const K &key) const
 Returns true if an element with key key exists.
 
const T * find (const K &key) const
 Returns a pointer to the element with key key or a null pointer if it is not found.
 
Array< K > keys () const
 Returns an array containing all keys of this map.
 
const T & operator[] (const K &key) const
 Returns a reference to the element with key key, or a static default constructed item if not found.
 
const T & get (const K &key, const T &def) const
 Returns the element with key key or the value def if key is not found.
 
Mapoperator() (const K &key, const T &value)
 Adds an element with the given key and value, useful for the short hand initializer style shown in the class overview.
 
bool remove (const K &key)
 Removes the element named key.
 
void add (const Map &d)
 Adds all elements from dictionary d to this.
 
Enumerator all () const
 Returns an enumerator for this map.
 
String join (const String &s1, const String &s2) const
 Joins the contents of a Dic<> into a string, using s1 as element separator (often a comma) and s2 as key-value separator (usually an '=').
 

Constructor & Destructor Documentation

◆ Map()

Map ( const Map< K2, T2 > &  b)
inline

Constructs a Map from a Map of different key or value types.

K2 and T2 must be convertible to K and T.


The documentation for this class was generated from the following file: