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;








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值