C++ Tutorial/STL Introduction/array algorithms — различия между версиями
Admin (обсуждение | вклад) м (1 версия: Импорт контента...) |
|
(нет различий)
|
Версия 14:21, 25 мая 2010
Содержание
- 1 Illustrating the generic adjacent_difference algorithm
- 2 Illustrating the generic inner_product algorithm with predicate
- 3 Illustrating the generic partial_sum algorithm
- 4 Use random_shuffle algorithms with array
- 5 Use the generic partition algorithms: Partition array1, putting numbers greater than 4 first, followed by those less than or equal to 4
- 6 Using function templates to get the max and min value in an array
- 7 Using reverse_copy to copy the array reversely
- 8 Using the STL generic reverse algorithm with an array
Illustrating the generic adjacent_difference algorithm
#include <iostream>
#include <cassert>
#include <algorithm>
#include <numeric>
using namespace std;
int main()
{
const int N = 20;
int x1[N], x2[N];
for (int i = 0; i < N; ++i)
x1[i] = i;
// Compute the partial sums of 0, 1, 2, 3, ..., N - 1, putting the result inx2:
partial_sum(&x1[0], &x1[N], &x2[0]);
// Compute the adjacent differences of elements in x2, placing the result back in x2:
adjacent_difference(&x2[0], &x2[N], &x2[0]);
for (int i = 0; i < N; ++i)
cout << x2[i] << " ";
return 0;
}
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Illustrating the generic inner_product algorithm with predicate
#include <algorithm>
#include <iostream>
#include <functional>
#include <numeric>
using namespace std;
int main()
{
const int N = 5;
int x1[N], x2[N];
for (int i = 0; i < N; ++i) {
x1[i] = i + 1;
x2[i] = i + 2;
}
// compute "inner product," with roles of + and * reversed:
int result = inner_product(&x1[0], &x1[N], &x2[0], 1,multiplies<int>(), plus<int>());
cout << "Inner product with roles of + and * reversed: " << result << endl;
return 0;
}
Inner product with roles of + and * reversed: 10395
Illustrating the generic partial_sum algorithm
#include <algorithm>
#include <numeric>
#include <iostream>
using namespace std;
int main()
{
const int N = 20;
int x1[N], x2[N];
for (int i = 0; i < N; ++i)
x1[i] = i;
// Compute the partial sums of 0, 1, 2, 3, ..., N - 1, putting the result inx2:
partial_sum(&x1[0], &x1[N], &x2[0]);
for (int i = 0; i < N; ++i)
cout << x2[i] << " ";
return 0;
}
0 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 136 153 171 190
Use random_shuffle algorithms with array
#include <iostream>
#include <algorithm>
#include <cassert>
#include <functional>
using namespace std;
int main() {
int a[100];
int i;
for (i = 0; i < 100; ++i)
a[i] = i;
random_shuffle(&a[0], &a[100]);
for (i = 0; i < 100; ++i)
cout << a[i] << " ";
return 0;
}
12 1 9 98 96 27 58 82 86 90 18 62 32 40 71 51 91 41 94 17 8 47 64 66 65 7 6 76 5 99 77 81 54 35 56 39 25 3 87 16 61 68 14 13 24 55 97 19 20 59 75 33 21 28 78 15 50 34 36 44 83 38 46 60 84 95 57 22 37 23 70 89 31 79 73 92 11 2 88 42 30 52 72 53 67 29 85 43 74 69 45 26 93 10 48 80 0 63 49 4 "
Use the generic partition algorithms: Partition array1, putting numbers greater than 4 first, followed by those less than or equal to 4
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <iterator>
using namespace std;
bool above40(int n) { return (n > 4); }
int main()
{
const int N = 7;
int array0[N] = {5, 3, 1, 7, 6, 4, 2};
int array1[N];
copy(&array0[0], &array0[N], &array1[0]);
ostream_iterator<int> out(cout, " ");
cout << "Original sequence: ";
copy(&array1[0], &array1[N], out); cout << endl;
int* split = partition(&array1[0], &array1[N], above40);
cout << "Result of (unstable) partitioning: ";
copy(&array1[0], split, out); cout << "| ";
copy(split, &array1[N], out); cout << endl;
return 0;
}
Original sequence: 5 3 1 7 6 4 2 Result of (unstable) partitioning: 5 6 7 | 1 3 4 2
Using function templates to get the max and min value in an array
#include <iostream>
using std::cout;
using std::endl;
template<class T> T max(const T* data, int size) {
T result = data[0];
for(int i = 1 ; i < size ; i++)
if(result < data[i])
result = data[i];
return result;
}
template<class T> T min(const T* data, int size) {
T result = data[0];
for(int i = 1 ; i < size ; i++)
if(result > data[i])
result = data[i];
return result;
}
int main() {
double data[] = {1.5, 4.6, 3.1, 1.1, 3.8, 2.1};
int numbers[] = {2, 22, 4, 6, 122, 12, 1, 45};
const int dataSize = sizeof data/sizeof data[0];
cout << "Minimum double is " << min(data, dataSize) << endl;
cout << "Maximum double is " << max(data, dataSize) << endl;
const int numbersSize = sizeof numbers/sizeof numbers[0];
cout << "Minimum integer is " << min(numbers, numbersSize) << endl;
cout << "Maximum integer is " << max(numbers, numbersSize) << endl;
return 0;
}
Minimum double is 1.1 Maximum double is 4.6 Minimum integer is 1 Maximum integer is 122
Using reverse_copy to copy the array reversely
#include <iostream>
#include <algorithm>
#include <cassert>
using namespace std;
int main() {
int a[100], b[100];
int i;
for (i = 0; i < 100; ++i)
a[i] = i;
reverse_copy(&a[0], &a[100], &b[0]);
for (i = 0; i < 100; ++i)
cout << " a: "<< a[i] << "b: " << b[i] << " \n";
return 0;
}
a: 0b: 99 a: 1b: 98 a: 2b: 97 a: 3b: 96 a: 4b: 95 a: 5b: 94 a: 6b: 93 a: 7b: 92 a: 8b: 91 a: 9b: 90 a: 10b: 89 a: 11b: 88 a: 12b: 87 a: 13b: 86 a: 14b: 85 a: 15b: 84 a: 16b: 83 a: 17b: 82 a: 18b: 81 a: 19b: 80 a: 20b: 79 a: 21b: 78 a: 22b: 77 a: 23b: 76 a: 24b: 75 a: 25b: 74 a: 26b: 73 a: 27b: 72 a: 28b: 71 a: 29b: 70 a: 30b: 69 a: 31b: 68 a: 32b: 67 a: 33b: 66 a: 34b: 65 a: 35b: 64 a: 36b: 63 a: 37b: 62 a: 38b: 61 a: 39b: 60 a: 40b: 59 a: 41b: 58 a: 42b: 57 a: 43b: 56 a: 44b: 55 a: 45b: 54 a: 46b: 53 a: 47b: 52 a: 48b: 51 a: 49b: 50 a: 50b: 49 a: 51b: 48 a: 52b: 47 a: 53b: 46 a: 54b: 45 a: 55b: 44 a: 56b: 43 a: 57b: 42 a: 58b: 41 a: 59b: 40 a: 60b: 39 a: 61b: 38 a: 62b: 37 a: 63b: 36 a: 64b: 35 a: 65b: 34 a: 66b: 33 a: 67b: 32 a: 68b: 31 a: 69b: 30 a: 70b: 29 a: 71b: 28 a: 72b: 27 a: 73b: 26 a: 74b: 25 a: 75b: 24 a: 76b: 23 a: 77b: 22 a: 78b: 21 a: 79b: 20 a: 80b: 19 a: 81b: 18 a: 82b: 17 a: 83b: 16 a: 84b: 15 a: 85b: 14 a: 86b: 13 a: 87b: 12 a: 88b: 11 a: 89b: 10 a: 90b: 9 a: 91b: 8 a: 92b: 7 a: 93b: 6 a: 94b: 5 a: 95b: 4 a: 96b: 3 a: 97b: 2 a: 98b: 1 a: 99b: 0
Using the STL generic reverse algorithm with an array
#include <iostream>
#include <string>
#include <cassert>
#include <algorithm> // For reverse algorithm
using namespace std;
int main()
{
char array1[] = "abc";
int N1 = strlen(array1);
reverse(&array1[0], &array1[N1]);
assert (string(array1) == "cba");
return 0;
}