SizeOf相關代碼

#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

using namespace std;

// 遍歷輸出
void PrintVec(const vector<int>& vec_)
{
    auto i_ = vec_.begin();
    while(i_ != vec_.end())
    {
        cout << * i_ << endl;
        ++i_;
    }
}

void TestSizeOf()
{
    char str[] = "Hello";
    char *p = str;
    int n = 10;

    // 求的是數組長度---
    cout << sizeof(str) << endl;

    // 求的是指針---- mac中指針佔8個字節
    cout << sizeof(p) << endl;

    // 求的是int int類型佔4個字節
    cout << sizeof(n) << endl;
}

// 空的類
class A
{
public:

};

class B
{
public:
    int m;
    int n;
};

class C
{
public:
    int m;
    char ch;
};

class D
{
public:
    //int m;
    char ch;
};

class E
{
public:
    int m;
    char ch;
    char *s;
};

// 虛函數 佔用一個指針的大小 在mac中表現爲8字節
class M
{
public:
    M(int x):a(x)
    {}

    virtual ~M();
private:
    int a;
};

// 測試類的字節對齊
void TestClassSizeOf()
{
    cout << sizeof(A) << endl;
    cout << sizeof(B) << endl;
    cout << sizeof(C) << endl;
    cout << sizeof(D) << endl;
    cout << sizeof(E) << endl;
    cout << sizeof(M) << endl;
}

// 測試Sort Int功能
void SortTest(vector<int>& vec_)
{
    sort(vec_.begin(),vec_.end());
    return;
}

int main() {
    vector<int> vec_ = {1,2,6,3,10,2,4,8};
    SortTest(vec_);
    //PrintVec(vec_);
    //std::cout << "Hello, World!" << std::endl;
    //TestSizeOf();
    TestClassSizeOf();
    return 0;
}

運行結果:
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章