Cynical news

Links
Literature
Music
Programming
Software
Programming
C++ Arrays
C++ Arrays
Initialise

Fixed contents int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
1 2 3 4 5
Repeat value 'e' 'n' times e=3;
n=5;
vector<int> a(n,e);
3 3 3 3 3

Insert

Prepend element 'e' int tmp[]={2,3,4};
vector<int> a(tmp,tmp+3);
e=1;
a.insert(a.begin(), e);
1 2 3 4
Append element 'e' int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
e=4;
a.push_back(e);
1 2 3 4
Insert element 'e' at index 'n' int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
n=3;
e=666;
a.insert(a.begin()+n,e);
1 2 3 666 4 5
Prepend array 'b' int tmp1[]={1,2,3,4,5};
int tmp2[]={10,20,30};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+3);
a .insert(a .begin() ,b.begin(),b.end());
10 20 30 1 2 3 4 5
Append array 'b' int tmp1[]={1,2,3,4,5};
int tmp2[]={10,20,30};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+3);
a .insert(a .end() ,b.begin(),b.end());
1 2 3 4 5 10 20 30
Insert array 'b' at index 'n' int tmp1[]={1,2,3,4,5};
int tmp2[]={10,20,30};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+3);
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' int tmp1[] = {1,2,3};
int tmp2[] = {4,5,6};
vector<int> a(tmp1,tmp1+3);
vector<int> b(tmp2,tmp2+3);
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' int tmp1[]={1,2,3,4,5};
int tmp2[]={0,2,4};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+3);
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' int tmp1[]={1,2,3};
vector<int> a(tmp1,tmp1+3);
e=666;
n=3;
m=1;
a .insert(a .begin()+m,n,e);
1 666 666 666 2 3

Overwrite

Overwrite first element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
n=0;
a.front()=666;
666 2 3
Overwrite last element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
n=0;
a.back()=666;
1 2 666
Overwrite element at index 'n' int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
n=1;
a[n]=666;
1 666 3
Overwrite elements from index 'n' (inclusive) int tmp1[]={1,2,3,4,5,6};
int tmp2[]={7,8,9};
int n=2;
vector<int> a(tmp1,tmp1+6);
vector<int> b(tmp2,tmp2+3);
copy(b .begin() ,b.end(),a.begin()+n);
1 2 7 8 9 6

Count

Length of array int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
n=a.size();
3
Size of underlying storage int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
a.resize(100);
n=a.capacity();
100
Number of element 'e' in array 'a' int tmp[]={1,2,2,3,3,3};
vector<int> a(tmp,tmp+6);
e=3;
n=count(a .begin(),a.end(),3);
3
Maximum element in array int tmp[]={1,2,300,4,5};
vector<int> a(tmp,tmp+5);
n=*max_element(a .begin(),a.end());
300
Minimum element in array int tmp[]={10,20,3,40,50};
vector<int> a(tmp,tmp+5);
n=*min_element(a .begin(),a.end());
3
Sum of elements in array int tmp[]={10,20,30,40};
vector<int> a(tmp,tmp+4);
n=accumulate(a .begin(),a.end(),0);
100

Retrieve by index

Retrieve first element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
e=a.front();
1
Retrieve first element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
e=a[0];
1
Retrieve last element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
e=a.back();
3
Retrieve last element int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
e=a[a.size()-1];
3
Retrieve element 'n' int tmp[]={1,2,3};
vector<int> a(tmp,tmp+3);
n=2;
e=a[n];
3
Retrieve out-of-bounds element 'n' int tmp[]={1,2,3};
n=4;
vector<int> a(tmp,tmp+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' int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
n=2;
a.erase(a.begin()+n);
1 2 4 5
Remove elements in index range 'm' to 'n' (inclusive) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
m=1;
n=3;
a.erase(a.begin()+m, a.begin()+n+1);
1 5
Remove elements from index 'n' onwards (inclusive) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
n=3;
a .erase(a .begin()+n,a.end());
1 2 3

Slice

Slice from index 'm' to 'n' (inclusive) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+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) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+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) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
n=3;
copy(a .begin() ,a .begin()+n+1,a.begin()); a.resize(n+1);
1 2 3 4

Search

Test element 'e' 'is in' array int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e=3;
t=(count(a .begin(),a.end(),e)>0);
1
Find index of element 'e' (forward) int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e=3;
n=find(a .begin() ,a.end(),e)-a.begin();
2

Sort

Sort in ascending order int tmp[]={5,1,4,3,2};
vector<int> a(tmp,tmp+5);
sort(a.begin(),a.end());
1 2 3 4 5
Sort in descending order int tmp[]={5,1,4,3,2};
vector<int> a(tmp,tmp+5);
sort(a .begin() ,a .end()) ; reverse(a .begin(),a.end());
5 4 3 2 1

Use as a stack

Push element 'e' int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e=666;
a.push_back(e);
1 2 3 4 5 666
Pop next element int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
a.pop_back();
1 2 3 4
Retrieve next element int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e = a.back();
5

Use as a FIFO

Add element 'e' int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e=666;
a.push_back(e);
1 2 3 4 5 666
Remove next element int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
a.erase(a.begin());
2 3 4 5
Retrieve next element int tmp[]={1,2,3,4,5};
vector<int> a(tmp,tmp+5);
e=a.front();
1

Compare

Test for equality (true) int tmp1[]={1,2,3,4,5};
int tmp2[]={1,2,3,4,5};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+5);
t=(a==b);
1
Test for equality (false) int tmp1[]={1,2,3,4,5};
int tmp2[]={1,2,3,4,50};
vector<int> a(tmp1,tmp1+5);
vector<int> b(tmp2,tmp2+5);
t=(a==b);
0
Test lexicographic 'a' < 'b' (true) int tmp1[]={1,2,2,2};
int tmp2[]={2,1,1,1};
vector<int> a(tmp1,tmp1+4);
vector<int> b(tmp2,tmp2+4);
t=(a<b);
1
Test lexicographic 'a' < 'b' (true) int tmp1[]={1,2,3};
int tmp2[]={1,2,3,4};
vector<int> a(tmp1,tmp1+3);
vector<int> b(tmp2,tmp2+4);
t=(a<b);
1
Test lexicographic 'a' < 'b' (false) int tmp1[]={1,2,2,2};
int tmp2[]={1,1,1,1};
vector<int> a(tmp1,tmp1+4);
vector<int> b(tmp2,tmp2+4);
t=(a<b);
0
Test lexicographic 'a' < 'b' (false) int tmp1[]={1,2,3,4};
int tmp2[]={1,2,3};
vector<int> a(tmp1,tmp1+4);
vector<int> b(tmp2,tmp2+3);
t=(a<b);
0

www.cynicalsoftware.com
14-11-09@15:37:24