Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Functions
Maps
Initialise
Insert
Overwrite
Count
Retrieve by position
Retrieve by key
Retrieve by value
Remove
Search
Iterate
Regex
Strings
Go
Arrays
Maps
Strings
Java
Arrays
Regex
Strings
JavaScript
Arrays
Functions
Objects
Regex
Strings
Perl
Arrays
Regex
Strings
Python
Arrays
Functions
Regex
Strings
C++ Maps
Initialise
Fixed contents n/a map<string,string> a = {
  { "one", "un" },
  { "two", "deux" }
}; //c++11
one=un two=deux
Custom key ordering // c++ functor to define
// map key comparison
struct Cmp
{
  bool operator()(
    const string& a,
    const string& b) const
  {
    return b < a;
  }
};
map<string,string,Cmp> a = {
  { "one", "un" },
  { "two", "deux" }
}; //c++11
two=deux one=un
Insert
Insert k,v pair map<string,string> m;
string k = "one";
string v = "un";
m[k]=v;
one=un
Insert k,v pair [alternative] map<string,string> a;
string k = "one";
string v = "un";
pair<string,string> p(k,v);
a.insert(p);
one=un
Insert k,v pair [alternative] map<string,string> a;
string k = "one";
string v = "un";
map<string,string>::value_type p(k,v);
a.insert(p);
one=un
Fail to insert k,v pair - pre-existing k map<string,string> a = {
  { "one", "un" },
  { "two", "deux" }
}; //c++11
string k = "one";
string v = "1";
pair<string,string> p(k,v);
a.insert(p);
one=un two=deux
Overwrite
Overwrite k,v pair map<string,string> a = {
  { "one", "un" },
  { "two", "deux" }
}; //c++11
string k = "one";
string v = "1";
a[k]=v;
one=1 two=deux
Count
Length of map map<string,string> a = {
  { "one", "un" },
  { "two", "deux" },
  { "three", "trois" }
}; //c++11
n=a.size();
3
Number of value 'v' in map map<string,string> a = {
  { "one", "foo" },
  { "two", "foo" },
  { "three", "bar" }
}; //c++11
string v = "foo";
typedef pair<string,string> Val;
n=count_if(
  a.begin(),
  a.end(),
  [&](const Val& e)
  {
    return e.second == v;
  } //c++11
);
2
Minimum key in map map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
n=a.begin()->first;
1
Minimum key in map [alternative] map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
typedef pair<int, int> Val;
n=min_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.first<y.first);
  } //c++11
)->first;
1
Minimum value in map map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
typedef pair<int, int> Val;
n=min_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.second<y.second);
  } //c++11
)->second;
100
Maximum key in map map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
n=a.rbegin()->first;
3
Maximum key in map [alternative] map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
typedef pair<int, int> Val;
n=max_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.first<y.first);
  } //c++11
)->first;
3
Maximum value in map map<int,int> a = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
typedef pair<int, int> Val;
n=max_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.second<y.second);
  } //c++11
)->second;
300
Retrieve by position
Retrieve first key map<int,string> a = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
int k=a.begin()->first;
1
Retrieve last key map<int,string> a = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
int k=a.rbegin()->first;
3
Retrieve value v associated with first key map<int,string> a = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
string v=a.begin()->second;
un
Retrieve value v associated with last key map<int,string> a = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
string v=a.rbegin()->second;
trois
Retrieve by key
Retrieve value v associated with key k map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
int k=2;
string v=a[k];
deux
Retrieve pair p with key k map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
pair<int,string> p;
int k=2;
p=*(a.find(k));
2=deux
Retrieve empty value map<string,int> a = {
  { "un", 1 },
  { "deux", 2 },
  { "trois", 3 }
}; //c++11
string k="quatre";
int v=a[k];
0
Retrieve by value
Retrieve first key k associated with value v map<int,string> a = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "orange" },
  { 4, "orange" },
  { 5, "orange" }
}; //c++11
string v="orange";
int k=-1;
map<int,string>::iterator i;
for (i = a.begin();
     i != a.end();
     ++i)
{
  if ( i->second == v )
  {
    k=i->first;
    break;
  }
}
3
Retrieve last key k associated with value v map<int,string> a = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "orange" },
  { 4, "orange" },
  { 5, "orange" }
}; //c++11
string v="orange";
int k=-1;
map<int,string>::reverse_iterator i;
for (i = a.rbegin();
     i != a.rend();
     ++i)
{
  if ( i->second == v )
  {
    k=i->first;
    break;
  }
}
5
Remove
Remove item by key k map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
int k=3;
a.erase(k);
1=un 2=deux
Remove item by iterator i map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
map<int,string>::iterator i=a.begin();
a.erase(i);
2=deux 3=trois
Remove items by iterator i whilst iterating over map map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
map<int,string>::iterator i;
i=a.begin();
while (i!=a.end())
{
  if (i->second == "un" ||
      i->second == "deux")
  {
    i=a.erase(i);//c++11
  }
  else
  {
    i++;
  }
}
3=trois
Remove items by iterator i whilst iterating over map [alternative] map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
map<int,string>::iterator i;
i=a.begin();
while (i!=a.end())
{
  if (i->second == "un" ||
      i->second == "deux")
  {
    // copy i before incremented
    map<int,string>::iterator j=i++;
    a.erase(j);
  }
  else
  {
    i++;
  }
}
3=trois
Search
Test element 'k' 'is in' map map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
};
int k=2;
bool t = ( a.find(k) != a.end() );
1
Test element 'k' 'is in' map [alternative] map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
};
int k=2;
bool t = ( a.count(k) > 0 );
1
Test element 'k' 'is in' map map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
};
int k=4;
bool t = ( a.find(k) != a.end() );
0
Test element 'k' 'is in' map [alternative] map<int,string> a = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
};
int k=4;
bool t = ( a.find(k) != a.end() );
0
Iterate
Iterate over map 'a' keys map<int,string> a = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "cherry" }
}; //c++11
vector<int> b;
for (map<int,string>::iterator i = a.begin();
     i != a.end();
     ++i)
{
  b.push_back(i->first);
}
1 2 3
Iterate over map 'a' values map<int,string> a = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "cherry" }
}; //c++11
vector<string> b;
for (map<int,string>::iterator i = a.begin();
     i != a.end();
     ++i)
{
  b.push_back(i->second);
}
apple banana cherry