array和vector的区别

 1 #include <iostream>
 2 #include <array>
 3 #include <vector>
 4 using namespace std;
 5 
 6 int main()
 7 {
 8     array<int, 10> a1 = {1,2,3,4,5,6,7,8,9,0};
 9     
10     std::cout << "sizeof(a1) = " << sizeof(a1) << std::endl;
11     for(auto it : a1)
12         std::cout << it << " ";
13     
14     std::cout << std::endl;
15     
16     vector<int> v1 = {1,2,3,4,5,6,7,8,9,0};
17     
18     std::cout << "sizeof(v1) = " << sizeof(v1) << std::endl;
19     
20     for(auto it : v1)
21         std::cout << it << " ";
22     
23     std::cout << std::endl;
24     
25     return 0;
26 }

运行结果:

 array的元素存在于栈上, 而vector只有元信息存在于栈上,数据存在于堆上。

原文地址:https://www.cnblogs.com/wanmeishenghuo/p/13551199.html