64,676
社区成员
发帖
与我相关
我的任务
分享
// TestVS2008.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
template <class T,size_t N>
struct block {
typedef T value_type;
typedef value_type* pointer;
typedef const value_type* const_pointer;
typedef value_type& reference;
typedef const value_type& const_reference;
typedef ptrdiff_t difference_type;
typedef size_t size_type;
typedef pointer iterator;
typedef const_pointer const_iterator;
iterator begin() { return data; }
iterator end() const { return data + N; }
reference operator[] (size_type n) { return data[n]; }
const_reference operator[] (size_type n) const { return data[n]; }
size_type size() const { return N; }
T data[N];
};
int _tmain(int argc, _TCHAR* argv[])
{
block<int,5> b = {1,2,3,4,5}; // b不是一个数组为什么可以这样初始化?
for (int i = 0; i < b.size() ; ++ i)
cout << b[i] << ' ';
return 0;
}
[Example:
struct A {
int x;
struct B {
int i;
int j;
} b;
} a = { 1, { 2, 3 } };
initializes a.x with 1, a.b.i with 2, a.b.j with 3. ]