Use of standard library array

#include <array>
#include <iostream>
using namespace std;

struct Point {
    int x, y;
};

ostream& operator<<(ostream& os, Point p)
{
    os << '{' << p.x << ',' << p.y << '}';
    return os;
}

template<typename T, int N>
void print(array<T, N>& a)
{
    for (int i = 0; i != N; ++i)
        cout << a[i] << '\n';
}

int main()
{
    array<Point, 3> points{ {{1,2},{3,4},{5,6}} };
    print<Point,3>(points);
    return 0;
}

The standard library array is declared as:

template<typename T, size_t N>
struct array{
    T elem[N];
    //other member functions
};

 

Guess you like

Origin www.cnblogs.com/lhb666aboluo/p/12724552.html