一:结构体
方式1
# include <bits/stdc++.h>
using namespace std; struct Node { string name; int grade;
} ; int main ( ) { Node * node = new Node ( ) ; node-> grade = 100 ; node-> name = "wyj" ; Node * node1 = new Node ( ) ; node1-> grade = 200 ; node1-> name = "wnd" ; cout << node-> name << ' ' << node-> grade << endl; cout << node1-> name << ' ' << node1-> grade; }
方式二
# include <bits/stdc++.h>
using namespace std; struct Node { string name; int grade;
} ; int main ( ) { Node node; node. grade = 100 ; node. name = "王大爷" ; cout << node. grade << ' ' << node. name << endl; ; Node node1; node1. grade = 100 ; node1. name = "王二爷" ; cout << node1. grade << ' ' << node1. name;
}
二:结构体数组
方式一
# include <bits/stdc++.h>
using namespace std; struct Node { string name; int grade;
} node[ 100 ] ; int main ( ) { for ( int i = 0 ; i < 2 ; i++ ) { cin >> node[ i] . name >> node[ i] . grade; } for ( int i = 0 ; i < 3 ; i++ ) { cout << node[ i] . name << ' ' << node[ i] . grade << endl; }
}
方式二:
# include <bits/stdc++.h>
using namespace std; struct Node { string name; int grade;
} ; int main ( ) { Node * stu = new Node[ 100 ] ; for ( int i = 0 ; i < 2 ; i++ ) { cin >> stu[ i] . name >> stu[ i] . grade; } for ( int i = 0 ; i < 2 ; i++ ) { cout << stu[ i] . name << ' ' << stu[ i] . grade << endl; }
}