1.
变量初始化的顺序要和定义的顺序一致,否则警告
struct Edge;struct Node { int dist; Edge *last; Node () : last(last), dist(0x7fffffff) {}} node[MAXN];
8 8 [Warning] 'Node::last' will be initialized after [-Wreorder]
7 6 [Warning] 'int Node::dist' [-Wreorder]
9 2 [Warning] when initialized here [-Wreorder]
9 2 [Warning] 'Node::last' is initialized with itself [-Winit-self]
2、结构体初始化
#includestruct Node { int a, b; int arry[5]; Node () { // 结构体初始化 a = 4; b = 0; for (int i = 1; i < 5; i++) arry[i] = 0; }} node[6];int main() { Node a; printf("%d\n", a.a); return 0;}
运行结果:
注意:如果想用 {} 初始化结构体的话,结构体中不能定义构造函数。
2、vector 初始化
转自:
二维vector
动态创建m*n的二维vector方法一:vector> ivec;ivec.resize(m);for(int i=0;i
方法二:
vector> ivec;ivec.resize(m,vector (n));
动态创建二维数组a[m][n]
C语言版:#includeint **a=(int **)malloc(m*sizeof(int *));for(int i=0;i
C++版:
int **a=new int*[m];for(int i=0;i
初始化二维数组
vector> ivec(m ,vector (n,0)); //m*n的二维vector,所有元素为0));