Programming
Bash
Arrays
Regex - grep
Strings
C
Arrays
Regex - POSIX
Strings
C++
Arrays
Initialise
Insert
Overwrite
Count
Retrieve by index
Remove by index
Slice
Iterate
Search
Sort
Use as stack
Use as FIFO
Compare
Remove by value
Bimaps
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++ Arrays
Initialise
Fixed contents n/a vector<int> a = {1,2,3,4,5}; //c++11
1 2 3 4 5
Repeat value 'e' 'n' times e=3;
n=5;
vector<int> a(n,e);
3 3 3 3 3
Initialise from native array int b[]={1,2,3,4,5};
vector<int> a(b,b+5);
1 2 3 4 5
Insert
Prepend element 'e' vector<int> a={2,3,4};
e=1;
a.insert(a.begin(), e);
1 2 3 4
Append element 'e' vector<int> a={1,2,3};
e=4;
a.push_back(e);
1 2 3 4
Insert element 'e' at index 'n' vector<int> a={1,2,3,4,5};
n=3;
e=666;
a.insert(a.begin()+n,e);
1 2 3 666 4 5
Prepend array 'b' vector<int> a={1,2,3,4,5};
vector<int> b={10,20,30};
a.insert(a.begin(),b.begin(),b.end());
10 20 30 1 2 3 4 5
Append array 'b' vector<int> a={1,2,3,4,5};
vector<int> b={10,20,30};
a.insert(a.end(),b.begin(),b.end());
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' vector<int> a={1,2,3,4,5};
vector<int> b={10,20,30};
n=3;
a.insert(a.begin()+n,b.begin(),b.end());
1 2 3 10 20 30 4 5
Concatenate array 'a' and array 'b' vector<int> a={1,2,3};
vector<int> b={4,5,6};
vector<int> c;
c.insert(c.begin(),a.begin(),a.end()); c.insert(c.end(),b.begin(),b.end());
1 2 3 4 5 6
Insert element 'e' at indices in 'b' vector<int> a={1,2,3,4,5};
vector<int> b={0,2,4};
e=666;
for (n=b.size()-1; n>=0; --n)
{
  a.insert(a.begin()+b[n],e);
}
666 1 2 666 3 4 666 5
Insert element 'e' 'n' times at index 'm' vector<int> a={1,2,3};
e=666;
n=3;
m=1;
a.insert(a.begin()+m,n,e);
1 666 666 666 2 3
Overwrite
Overwrite first element vector<int> a={1,2,3};
n=0;
a.front()=666;
666 2 3
Overwrite last element vector<int> a={1,2,3};
n=0;
a.back()=666;
1 2 666
Overwrite element at index 'n' vector<int> a={1,2,3};
n=1;
a[n]=666;
1 666 3
Overwrite elements from index 'n' (inclusive) int n=2;
vector<int> a={1,2,3,4,5,6};
vector<int> b={7,8,9};
copy(b.begin(),b.end(),a.begin()+n);
1 2 7 8 9 6
Overwrite all elements vector<int> a={1,2,3,4,5,6};
vector<int> b={7,8,9};
a=b;
7 8 9
Count
Length of array vector<int> a={1,2,3};
n=a.size();
3
Size of underlying storage vector<int> a={1,2,3};
a.resize(100);
n=a.capacity();
100
Number of element 'e' in array 'a' vector<int> a={1,2,2,3,3,3};
e=3;
n=count(a.begin(),a.end(),3);
3
Maximum element in array vector<int> a={1,2,300,4,5};
n=*max_element(a.begin(),a.end());
300
Minimum element in array vector<int> a={10,20,3,40,50};
n=*min_element(a.begin(),a.end());
3
Sum of elements in array vector<int> a={10,20,30,40};
n=accumulate(a.begin(),a.end(),0);
100
Product of elements in array vector<int> a={1,2,3,4,5};
n=accumulate(
  a.begin(),
  a.end(),
  1,
  multiplies<int>());
120
Retrieve by index
Retrieve first element vector<int> a={1,2,3};
e=a.front();
1
Retrieve first element [alternative] vector<int> a={1,2,3};
e=a[0];
1
Retrieve last element vector<int> a={1,2,3};
e=a.back();
3
Retrieve last element [alternative] vector<int> a={1,2,3};
e=a[a.size()-1];
3
Retrieve element 'n' vector<int> a={1,2,3};
n=2;
e=a[n];
3
Retrieve out-of-bounds element 'n' n=4;
vector<int> a={1,2,3};
try
{
  e=a.at(n);
}
catch ( std::out_of_range e )
{
  es="out of bounds";
}
out of bounds
Remove by index
Remove element at index 'n' vector<int> a={1,2,3,4,5};
n=2;
a.erase(a.begin()+n);
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) vector<int> a={1,2,3,4,5};
m=1;
n=3;
a.erase(a.begin()+m, a.begin()+n+1);
1 5
Remove elements from index 'n' onwards (inclusive) vector<int> a={1,2,3,4,5};
n=3;
a.erase(a.begin()+n,a.end());
1 2 3
Slice
Slice from index 'm' to 'n' (inclusive) vector<int> a={1,2,3,4,5};
m=1;
n=3;
copy(a.begin()+m,a.begin()+n+1,a.begin()); a.resize(n-m+1);
2 3 4
Slice from index 'n' onwards (inclusive) vector<int> a={1,2,3,4,5};
n=2;
copy(a.begin()+n,a.end(),a.begin()); a.resize(a.size()-n);
3 4 5
Slice up to index 'n' (inclusive) vector<int> a={1,2,3,4,5};
n=3;
copy(a.begin(),a.begin()+n+1,a.begin()); a.resize(n+1);
1 2 3 4
Iterate
Iterate over array 'a' vector<int> a={1,2,3,4,5};
vector<int> b;
for (int i=0; i < a.size(); i++)
{
  b.push_back(a[i]*2);
}
2 4 6 8 10
Iterate over array 'a' [alternative] vector<int> a={1,2,3,4,5};
vector<int> b;
vector<int>::iterator i = a.begin();
while ( i != a.end() )
{
  b.push_back((*i)*2);
  i++;
}
2 4 6 8 10
Iterate over array 'a' [alternative] vector<int> a={1,2,3,4,5};
vector<int> b;
for (int n : a)
{
  b.push_back(n*2);
}
2 4 6 8 10
Iterate over array 'a' and 'b' in lockstep vector<int> a = {1,3,5};
vector<int> b = {2,4,6};
vector<int> c;
// Composite parallel iterator
auto i =
  boost::make_zip_iterator(
  boost::make_tuple(
    a.begin(),
    b.begin())
);
auto j =
  boost::make_zip_iterator(
  boost::make_tuple(
    a.end(),
    b.end())
  );
for ( ; i != j; ++i )
{
  c.push_back(
    boost::get<0>(*i));
  c.push_back(
    boost::get<1>(*i));
}
1 2 3 4 5 6
Iterate over array 'a' and 'b' sequentially vector<int> a = {1,2,3};
vector<int> b = {4,5,6};
vector<int> c;
// Composite sequential iterator
auto j = boost::join(a, b);
for (
  auto itt = boost::begin(j);
  itt != boost::end(j);
  ++itt)
{
  c.push_back(*itt);
}
1 2 3 4 5 6
Iterate over array 'a' removing element 'e' vector<int> a={1,2,3,4,5};
int e=3;
for (auto it=a.begin();
     it!=a.end();
     )
{
  if ( *it == e )
  {
    it = a.erase(it);
  }
  else
  {
    ++it;
  }
}
1 2 4 5
Search
Test element 'e' 'is in' array vector<int> a={1,2,3,4,5};
e=3;
t=(count(a.begin(),a.end(),e)>0);
1
Find index of unique element 'e' vector<int> a={1,2,3,4,5};
e=3;
n=find(a.begin(),a.end(),e)-a.begin();
2
Find index of first element 'e' vector<int> a={1,2,666,3,4,666,5};
e=666;
n=find(a.begin(),a.end(),e)-a.begin();
2
Find index of last element 'e' vector<int> a = {1,2,666,3,4,666,5};
e=666;
n=find(a.rbegin(),a.rend(),e)-a.rbegin();
n=a.size()-n-1;// convert to forward index
5
Sort
Sort in ascending order vector<int> a={5,1,4,3,2};
sort(a.begin(),a.end());
1 2 3 4 5
Sort in descending order vector<int> a={5,1,4,3,2};
sort(a.begin(),a.end());
reverse(a.begin(),a.end());
5 4 3 2 1
Reverse vector<int> a={1,2,3,4,5};
reverse(a.begin(),a.end());
5 4 3 2 1
Use as a stack
Push element 'e' vector<int> a={1,2,3,4,5};
e=666;
a.push_back(e);
1 2 3 4 5 666
Pop next element vector<int> a={1,2,3,4,5};
a.pop_back();
1 2 3 4
Retrieve next element vector<int> a={1,2,3,4,5};
e = a.back();
5
Use as a FIFO
Add element 'e' vector<int> a={1,2,3,4,5};
e=666;
a.push_back(e);
1 2 3 4 5 666
Remove next element vector<int> a={1,2,3,4,5};
a.erase(a.begin());
2 3 4 5
Retrieve next element vector<int> a={1,2,3,4,5};
e=a.front();
1
Compare
Test for equality (true) vector<int> a={1,2,3,4,5};
vector<int> b={1,2,3,4,5};
t=(a==b);
1
Test for equality (false) vector<int> a={1,2,3,4,5};
vector<int> b={1,2,3,4,50};
t=(a==b);
0
Test lexicographic 'a' < 'b' (true) vector<int> a={1,2,2,2};
vector<int> b={2,1,1,1};
t=(a<b);
1
Test lexicographic 'a' < 'b' (true) vector<int> a={1,2,3};
vector<int> b={1,2,3,4};
t=(a<b);
1
Test lexicographic 'a' < 'b' (false) vector<int> a={1,2,2,2};
vector<int> b={1,1,1,1};
t=(a<b);
0
Test lexicographic 'a' < 'b' (false) vector<int> a={1,2,3,4};
vector<int> b={1,2,3};
t=(a<b);
0
Remove by value
Remove first element with value 'n' vector<int> a={1,2,666,3,4,666,5};
n=666;
auto it = find(a.begin(), a.end(), n);
a.erase(it);
1 2 3 4 666 5
Remove last element with value 'n' vector<int> a={1,2,666,3,4,666,5};
n=666;
auto it = find(a.rbegin(), a.rend(), n);
// Reverse iterator cannot be passed to erase,
// advance one and convert to forward iterator.
a.erase((++it).base());
1 2 666 3 4 5
Remove all elements with value 'n' vector<int> a={1,2,666,3,4,666,5};
n=666;
// This is the c++ erase-remove idiom
a.erase(
  remove(
    a.begin(),
    a.end(),
    n),
  a.end());
1 2 3 4 5
Remove first element with value matching condition 'f' bool is_even(int i)
{
  return (i % 2) == 0;
}
vector<int> a={1,2,3,4,5};
auto f=is_even;
auto it = find_if(a.begin(), a.end(), f);
a.erase(it);
1 3 4 5
Remove last element with value matching condition 'f' vector<int> a={1,2,3,4,5};
auto f=is_even;
auto it = find_if(a.rbegin(), a.rend(), f);
// Reverse iterator cannot be passed to erase,
// advance one and convert to forward iterator.
a.erase((++it).base());
1 2 3 5
Remove all elements with value matching condition 'f' bool is_odd(int i)
{
  return (i % 2) != 0;
}
vector<int> a={1,2,3,4,5,6};
auto f=is_odd;
// This is the c++ erase-remove idiom
a.erase(
  remove_if(
    a.begin(),
    a.end(),
    f),
  a.end());
2 4 6