, . ArrayWithMax
, int*
, , int const *
. non-mutable ArrayWithMax
ArrayWithMax::Max()
, . , , , vlad_tepesch , . , :
#include <iostream>
#include <algorithm>
#include <cstring>
#include <iterator>
using namespace std;
const size_t ARRAY_SIZE = 2;
template <typename T>
class ArrayWithMax
{
public:
ArrayWithMax(T array) : array_(array){}
template <typename U>
void Max(const U& rhs)
{
transform(begin(), end(), rhs.begin(), begin(),
[] (auto a, auto b) { return max(a, b); } );
}
T begin() { return array_; }
T end() { return array_+size(); }
T begin() const { return array_; }
T end() const { return array_+size(); }
size_t size() const { return ARRAY_SIZE; }
private:
T array_;
};
int main()
{
int someArray[ARRAY_SIZE];
someArray[0] = 1;
someArray[1] = 2;
int const * received = someArray;
int someArray2[ARRAY_SIZE];
someArray2[0] = 0;
someArray2[1] = 8;
int const * received2 = someArray2;
int myStorage[ARRAY_SIZE];
memset(myStorage, 0, sizeof(myStorage));
int* aWithMax = myStorage;
ArrayWithMax<int*> a(aWithMax);
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << endl;
ArrayWithMax<const int*> receivedArray(received);
a.Max(receivedArray);
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << endl;
ArrayWithMax<const int*> receivedArray2(received2);
a.Max(receivedArray2);
copy(a.begin(), a.end(), ostream_iterator<int>(cout, " "));
cout << endl;
}
, , :
Luiss-Air:const luis$ g++-5.3.0 -std=c++14 ArrayWithMax.C
Luiss-Air:const luis$ a.out
0 0
1 2
1 8
Luiss-Air:const luis$
ArrayWithMax
, std:: transform:
transform(aWithMax, aWithMax+ARRAY_SIZE, received, aWithMax,
[] (auto a, auto b) { return max(a, b); });