Vectors are Containers for Objects
sample programs of these are on the web


Vector Functions


Don't forget to include:

#include // include the "algorithm" library
#include // include the "vector" library
using namespace std; // use standard names

Examples of reordering algorithms operating on complete vectors:

reverse(v.begin(), v.end());
random_shuffle(v.begin(), v.end());
rotate(v.begin(), v.begin()+4, v.end());
next_permutation(next_m1.begin(), next_m1.end(),less());
sort(v1.begin(), v1.end());

Examples of equivalent reordering algorithms operating on portions of vectors:

int begin = 3;
int end = 7;
int array[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
vector vArray(array, array + 10);

random_shuffle(&vArray.at(begin), &vArray.at(end));
random_shuffle(&vArray[begin], &vArray[end]);
random_shuffle(vArray.begin()+begin, vArray.begin()+end);

reverse(&vArray.at(begin), &vArray.at(end));
reverse(&vArray[begin], &vArray[end]);
reverse(vArray.begin()+begin, vArray.begin()+end);

rotate(&vArray.at(begin), &vArray.at(end), vArray.end());
rotate(&vArray[begin], &vArray[end], vArray.end());
rotate(vArray.begin()+begin, vArray.begin()+end, vArray.end());

sort(&vArray.at(begin), &vArray.at(end));
sort(&vArray[begin], &vArray[end]);
sort(vArray.begin()+begin, vArray.begin()+end);

Also see Borland "Help" under:


Putting a 1-D array into a Vector:

Original
int array[10];
Original
vector v(array, array+10);
Reordered
vector v();
Reordered
int array[10];
0
0
9
9
1
1
0
0
2
2
1
1
3
3
8
8
4
4
7
7
5
5
6
6
6
6
4
4
7
7
5
5
8
8
3
3
9
9
2
2

// Array declaration and definition
int array[10] = {0,1,2,3,4,5,6,7,8,9};

// Vector declaration and definition
vector v(array, array+10);

// These are all valid function calls
reverse(v.begin(), v.end());
random_shuffle(v.begin(), v.end());
rotate(v.begin(), v.begin() + 1, v.end());
next_permutation(v.begin(), v.end());
sort(v.begin(), v.end());

// This copies the "vector" back to the "array"
for (int i = 0; i < 10; i++) {
array[i] = v[i];
}


Putting a 2-D array into a Vector:

Original
int array[10][2];
Original
int index[10];
Original
vector v(index, index+10);
Reordered
vector v();
Temporary Reordered
int tempArray[10][2];
Reordered
int array[10][2];
0, 10
0
0
9
9, 19
9, 19
1, 11
1
1
0
0, 10
0, 10
2, 12
2
2
1
1, 11
1, 11
3, 13
3
3
8
8, 18
8, 18
4, 14
4
4
7
7, 17
7, 17
5, 15
5
5
6
6, 16
6, 16
6, 16
6
6
4
4, 14
4, 14
7, 17
7
7
5
5, 15
5, 15
8, 18
8
8
3
3, 13
3, 13
9, 19
9
9
2
2, 12
2, 12

// Coordinate array declaration and definition
int array[10][2] = {0, 10, 1, 11, 2, 12, 3, 13, 4, 14,
5, 15, 6, 16, 7, 17, 8, 18, 9, 19};

// Temporary coordinate array declaration and definition
int tempArray[10][2];

// Index array declaration and definition

int index[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

// Vector declaration and definition
vector v(index, index+10);

// These are all valid function calls

reverse(v.begin(), v.end());
random_shuffle(v.begin(), v.end());
rotate(v.begin(), v.begin() + 1, v.end());
next_permutation(v.begin(), v.end());
sort(v.begin(), v.end());

// This copies the "vector" back to the "array"

// use the VECTOR indices and the original array to create a tempArray
for (int i = 0; i < 10; i++) {
tempArray[i][0] = array[v[i]][0];
tempArray[i][1] = array[v[i]][1];
}
// copy the tempArray back to the original array
for (int i = 0; i < 10; i++) {
array[i][0] = tempArray[i][0];
array[i][1] = tempArray[i][1];
}


Putting a TPoint structure array into a Vector:

Original
TPoint city[10];
Original
vector vcity(city, city+10);
Reordered
vector vcity();
Reordered
TPoint city[10];
0, 10
0
9
9, 19
1, 11
1
0
0, 10
2, 12
2
1
1, 11
3, 13
3
8
8, 18
4, 14
4
7
7, 17
5, 15
5
6
6, 16
6, 16
6
4
4, 14
7, 17
7
5
5, 15
8, 18
8
3
3, 13
9, 19
9
2
2, 12

 

// TPoint declaration and definition
TPoint city[10] = {Point(0, 10), Point(1, 11), Point(2, 12), Point(3, 13), Point(4, 14),
Point(5, 15), Point (6, 16), Point(7, 17), Point (8, 18), Point(9, 19)};

//Vector declaration and definition
vector vcity(city, city+10);

// These are all valid function calls
reverse(vcity.begin(), vcity.end());
random_shuffle(vcity.begin(), vcity.end());
rotate(vcity.begin(), vcity.begin() + 1, vcity.end());

// CAUTION: Do not attempt to call either sort() or next_permutation(). These are undefined procedures.

// This copies the "vector" back to the "array"
for (int i = 0; i < 10; i++) {
city[i].x = vcity[i].x; city[i].y = vcity[i].y;
}