C++ 复合类型
struct Student { std::string name; int age; }; //定义Student1的时候创建变量std1 struct Student1 { std::string name; int age; }std1; //定义无名机构体的时候创建变量std0 struct { std::string name; int age; }std0; struct _student { std::string name; int age; _student(std::string _name,int _age) { name = _name; age = _age; } };
int num[3]; num[0] = 1; num[1] = 2; num[2] = 3; for(int i : num) { log("This is %d", i); } int arr[3] = {4, 5, 6}; log("Size of int is %lu", sizeof(int));//4 log("Size of arr is %lu", sizeof(arr));//12 int arr1[] = {7,8,9};//编译器会帮你算个数的,可是不推荐。 int arr2[100] = {0};//仅仅要显示的初始化第一个元素,编译器就会把其它的元素都初始化为0 //c++11新特性 int c11_1[3] {5, 2, 0};//能够省略等于号 int c11_2[3] {};//能够不在括号中写不论什么东西,这将把全部元素都设置为0 std::string love {"zhouyunxuan"}; //struct std1.name = "yunxuan"; std1.age = 21; Student stu2 = { "zhouyunxuan", 21 }; Student stu3 {"yunxuan", 21}; //创建动态数组 int idx = 10; int* p = new int[idx]; for (int i = 0; i < 10; ++i) { p[i] = i*100; } for (int i = 0; i < 10; ++i) { log("this is %d", p[i]); } _student ss("YUNXUAN", 21); log("my name is %s", ss.name.c_str());
//结构体函数 typedef struct _student { std::string name; int age; _student(const char * _name):name(_name){ printf("%s\n", name.c_str()); } }Student;