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;








發佈了39 篇原創文章 · 獲贊 4 · 訪問量 26萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章