复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110#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; }
运行结果:
最后
以上就是飞快枫叶最近收集整理的关于SizeOf相关代码的全部内容,更多相关SizeOf相关代码内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复