首页 > 代码库 > C++ 复合类型
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;
C++ 复合类型
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。