To convert a vector to an array?

Asked 2 years ago, Updated 2 years ago, 140 views

vector that stores double I want to convert double to an array that stores it. What function should I use?

c++ array vector

2022-09-22 22:24

1 Answers

vector stores elements in memory space continuously just like an array All you have to do is point at the vector with the pointer Please write as follows

int main(){
    std::vector<double> v;
    v.push_back(3.14);
    v.push_back(5.16);
    double* a = &v[0]; // Like this

    for (int i=0; i<2; i++) {
        cout << a[i] << endl;
    }
}


2022-09-22 22:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.