一个数组外包装,错在哪里?
xlsue 2005-09-21 01:58:30 //数组外包装
#include <iostream>
#include <algorithm>
#include <cstddef>
using namespace std;
template <typename T, size_t thesize>
class carray {
private:
T v[thesize];
public:
typedef T value_type;
typedef T* iterator;
typedef const T* const_iterator;
typedef T& reference;
typedef const T& const_reference;
typedef size_t size_type;
typedef ptrdiff_t difference_type;
iterator begin() { return v;}
const_iterator begin() { return v;}
iterator end() { return v+thesize; }
const_iterator end() { return v+thesize;}
reference operator[](size_t i) { return v[i]; }
const_reference operator[](size_t i) const { return v[i]; }
size_type size() const { return thesize; }
size_type max_size() const { return thesize; }
T* as_array() { return v; }
};
int main()
{
carray<int, 10> a;
for(size_t i=0; i<a.size(); ++i)
a[i] = i+1;
for(size_t i=0; i<a.size(); ++i)
cout << a[i] << " ";
cout << endl;
reverse(a.begin(), a.end());
for(size_t i=0; i<a.size(); ++i)
cout << a[i] << " ";
cout << endl;
getchar();
return 0;
}