Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Bimaps
Initialise
Insert
Overwrite
Count
Retrieve by position
Retrieve by key
Retrieve by value
Remove
Functions
Maps
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++ Bimaps
Initialise
Fixed contents typedef bimap<
  string,
  string
  > Map;
typedef Map::value_type Val;
// bimap does not support
// initialiser syntax so
// use a vector to init
vector<Val> i {
  {"one", "un"},
  {"two", "deux"}
}; //c++11
Map a(i.begin(),i.end());
one=un two=deux
Custom key ordering // c++ functor to define
// map key comparison
template<class ItemType>
struct Cmp
{
  bool operator()(
    ItemType a,
    ItemType b) const
  {
    return b.left < a.left;
  }
};
typedef bimap<
  string,
  string,
  set_of_relation<Cmp<_relation>>
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  { "one", "un" },
  { "two", "deux" }
}; //c++11
Map a(i.begin(),i.end());
two=deux one=un
Unique key and unique reverse key (value) n/a typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {1, "alice"},
  {2, "bob"},
  {2, "carole"}, //ignored
  {3, "bob"} //ignored
}; //c++11
Map a(i.begin(),i.end());
1=alice 2=bob
Unique key and duplicate reverse keys (values) n/a typedef bimap<
  int,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {1, "alice"},
  {2, "bob"},
  {2, "carole"}, //ignored
  {3, "bob"} //ok
}; //c++11
Map a(i.begin(),i.end());
1=alice 2=bob 3=bob
Duplicate keys and duplicate reverse keys (values) n/a typedef bimap<
  list_of<int>,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {1, "alice"},
  {2, "bob"},
  {2, "carole"}, //ok
  {3, "bob"} //ok
}; //c++11
Map a(i.begin(),i.end());
1=alice 2=bob 2=carole 3=bob
Insert
Insert k,v pair // list_of is mutable which
// allows use of [] operator
typedef bimap<
  string,
  list_of<string>
  > Map;
Map m;
string k = "one";
string v = "un";
m.left[k]=v; //needs mutable Val
one=un
Insert k,v pair [alternative] typedef bimap<
  string,
  string
  > Map;
typedef Map::value_type Val;
Map a;
string k = "one";
string v = "un";
Val p(k,v);
a.insert(p);
one=un
Fail to insert k,v pair - pre-existing k typedef bimap<
  string,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {"one", "un"},
  {"two", "deux"}
}; //c++11
Map a(i.begin(),i.end());
string k = "one";
string v = "1";
Val p(k,v);
a.insert(p);
one=un two=deux
Overwrite
Overwrite k,v pair by key // list_of is mutable which
// allows use of [] operator
typedef bimap<
  string,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {"one", "un"},
  {"two", "deux"}
}; //c++11
Map a(i.begin(),i.end());
string k = "one";
string v = "1";
a.left[k]=v;//needs mutable val
one=1 two=deux
Overwrite k,v pair by value // list_of is mutable which
// allows use of [] operator
typedef bimap<
  list_of<string>,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  {"one", "un"},
  {"two", "deux"}
}; //c++11
Map a(i.begin(),i.end());
string k = "1";
string v = "un";
a.right[v]=k;//needs mutable key
1=un two=deux
Count
Length of map typedef bimap<
  string,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  { "one", "un" },
  { "two", "deux" },
  { "three", "trois" }
}; //c++11
Map a(i.begin(), i.end());
n=a.size();
3
Number of value 'v' in map // list_of is needed to
// allow duplicate items
// on right of map, e.g. "foo"
typedef bimap<
  string,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> i {
  { "one", "foo" },
  { "two", "foo" },
  { "three", "bar" }
}; //c++11
Map a(i.begin(), i.end());
string v = "foo";
n=count_if(
  a.begin(),
  a.end(),
  [&](const Val& e)
  {
    return e.right == v;
  } //c++11
);
2
Minimum key in map typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=a.left.begin()->first;
1
Minimum key in map [alternative] typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=min_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.left<y.left);
  } //c++11
)->left;
1
Minimum value in map typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=min_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.right<y.right);
  } //c++11
)->right;
100
Maximum key in map typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=a.rbegin()->left;
3
Maximum key in map [alternative] typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=max_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.left<y.left);
  } //c++11
)->left;
3
Maximum value in map typedef bimap<
  int,
  int> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 3, 300 },
  { 2, 200 },
  { 1, 100 }
}; //c++11
Map a(i.begin(), i.end());
n=max_element(
  a.begin(),
  a.end(),
  [](const Val& x, const Val& y)
  {
    return (x.right<y.right);
  } //c++11
)->right;
300
Retrieve by position
Retrieve first key typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
int k=a.left.begin()->first;
1
Retrieve last key typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
int k=a.left.rbegin()->first;
3
Retrieve value v associated with first key typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
string v=a.left.begin()->second;
un
Retrieve value v associated with last key typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
string v=a.left.rbegin()->second;
trois
Retrieve first value typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
string v=a.right.begin()->first;
deux
Retrieve last value typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
string v=a.right.rbegin()->first;
un
Retrieve key k associated with first value typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
int k=a.right.begin()->second;
2
Retrieve key k associated with last value typedef bimap<
  int,
  string> Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 3, "trois" },
  { 2, "deux" }
}; //c++11
Map a(i.begin(), i.end());
int k=a.right.rbegin()->second;
1
Retrieve by key
Retrieve value v associated with key k typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(i.begin(), i.end());
int k=2;
string v=a.left.at(k);
deux
Retrieve value v associated with key k [alternative] // list_of is mutable which
// allows use of [] operator
typedef bimap<
  int,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(i.begin(), i.end());
int k=2;
string v=a.left[k];//needs mutable Val
deux
Retrieve pair p with key k typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(i.begin(), i.end());
int k=2;
Map::left_iterator itt=a.left.find(k);
//auto p=*itt; // TODO: find correct type
2=deux
Retrieve by value
Retrieve first key k associated with value v // list_of allows multiple pairs
// with same val, e.g. "orange"
// but right.at not supported
typedef bimap<
  int,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "orange" },
  { 4, "orange" },
  { 5, "orange" }
}; //c++11
Map a(j.begin(), j.end());
string v="orange";
int k=-1;
Map::right_iterator i;
for (i = a.right.begin();
     i != a.right.end();
     ++i)
{
  if ( i->first == v )
  {
    k=i->second;
    break;
  }
}
3
Retrieve first key k associated with value v [alternative] // without list_of only single
// pair with specific val is
// stored, e.g. "orange"
typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "orange" },
  { 4, "orange" }, // ignored
  { 5, "orange" } // ignored
}; //c++11
Map a(j.begin(), j.end());
string v="orange";
int k=a.right.at(v);
3
Retrieve last key k associated with value v // list_of allows multiple pairs
// with same val, e.g. "orange"
// but right.at not supported
typedef bimap<
  int,
  list_of<string>
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "apple" },
  { 2, "banana" },
  { 3, "orange" },
  { 4, "orange" },
  { 5, "orange" }
}; //c++11
Map a (j.begin(), j.end());
string v="orange";
int k=-1;
Map::right_reverse_iterator i;
for (i = a.right.rbegin();
     i != a.right.rend();
     ++i)
{
  if ( i->first == v )
  {
    k=i->second;
    break;
  }
}
5
Retrieve last key k associated with value v [alternative] // without list_of only single
// pair with specific val is
// stored, e.g. "orange"
typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "apple" },
  { 2, "banana" },
  // first value for specific
  // key used, others ignored
  { 5, "orange" }
}; //c++11
Map a(j.begin(), j.end());
string v="orange";
int k=a.right.at(v);
5
Remove
Remove item by key k typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> i = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
};
Map a(i.begin(), i.end());
int k=3;
a.left.erase(k);
1=un 2=deux
Remove item by key iterator i typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(j.begin(), j.end());
Map::left_iterator i=a.left.begin();
a.left.erase(i);
2=deux 3=trois
Remove items by key iterator i whilst iterating over map typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> j = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(j.begin(), j.end());
Map::left_iterator i;
i=a.left.begin();
while (i!=a.left.end())
{
  if (i->second == "un" ||
      i->second == "deux")
  {
    i=a.left.erase(i);
  }
  else
  {
    i++;
  }
}
3=trois
Remove items by key iterator i whilst iterating over map [alternative] typedef bimap<
  int,
  string
  > Map;
typedef Map::value_type Val;
vector<Val> k = {
  { 1, "un" },
  { 2, "deux" },
  { 3, "trois" }
}; //c++11
Map a(k.begin(), k.end());
Map::left_iterator i;
i=a.left.begin();
while (i!=a.left.end())
{
  if (i->second == "un" ||
      i->second == "deux")
  {
    // copy i before incremented
    Map::left_iterator j=i++;
    a.left.erase(j);
  }
  else
  {
    i++;
  }
}
3=trois