c++包容器类和模板

#include <assert.h>
#include <iostream>
#include <string>
using namespace std;
template<class T>
class arrayT {
    enum {
        size = 100
    };
    T A[size];
public:
    T& operator [](int index) {
        assert(index >= 0 && index < size);
        return A[index];
    }
};
int main()
{
    arrayT<int> ia;
    arrayT<float> fa;
    for (int i = 0; i < 20; i++) {
        ia[i] = i * i;
        fa[i] = float(i)*1.414;
    }
    for (int i = 0; i < 20; i++) {
        cout << ia[i] << endl;
        cout << fa[i] << endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/yinghonghui/article/details/82724738
今日推荐