代碼如下:
#include <iostream>
#include <array>
#include <vector>
using namespace std;
int main(int argc, const char * argv[]) {
//1.vector模板類
vector<double> vec(3);
vec[0] = 2.33;
vec[1] = 3.33;
vec[2] = 4.33;
cout << "There are vector numbers :" << endl;
for (int i = 0; i < 3; i++) {
cout << vec[i] << " address is :" << &vec[i] << endl;
}
//2.array模板類
array<float, 3> arr = {1.1,2.2,3.3};
// arr[0] = 1.1;
// arr[1] = 2.2;
// arr[3] = 3.3;
cout << "There are array numbers :" << endl;
for (int j = 0; j < 3 ; j++) {
cout << arr[j] << " address is :" << &arr[j] << endl;
}
return 0;
}
輸出結(jié)果:
There are vector numbers :
2.33 address is :0x100300330
3.33 address is :0x100300338
4.33 address is :0x100300340
There are array numbers :
1.1 address is :0x7fff5fbff5f8
2.2 address is :0x7fff5fbff5fc
3.3 address is :0x7fff5fbff600
Program ended with exit code: 0
1.vector模板類
使用vector模板類首先要導(dǎo)入頭文件#include <vector>
聲明
vector<typeName> name(ele_num);
2.array模板類
使用array模板類需要導(dǎo)入#include <array>
聲明
array<typeName,ele_num> name;