数组的替代品

代码如下:
#include 
#include 
#include 

using namespace std;
int main(int argc, const char * argv[]) {

    //1.vector模板类
    vector 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 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;
}
输出结果:
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模板类首先要导入头文件#include

声明

vector name(ele_num);

2.array模板类

使用array模板类需要导入#include

声明

array name;

你可能感兴趣的:(数组的替代品)