Using arrays or std::vectors in C++, what's the performance gap?

后端 未结 19 1783
忘了有多久
忘了有多久 2020-11-22 03:23

In our C++ course they suggest not to use C++ arrays on new projects anymore. As far as I know Stroustroup himself suggests not to use arrays. But are there significant perf

19条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 04:21

    To respond to something Mehrdad said:

    However, there might be cases where you still need arrays. When interfacing with low level code (i.e. assembly) or old libraries that require arrays, you might not be able to use vectors.

    Not true at all. Vectors degrade nicely into arrays/pointers if you use:

    vector vector;
    vector.push_back(42);
    
    double *array = &(*vector.begin());
    
    // pass the array to whatever low-level code you have
    

    This works for all major STL implementations. In the next standard, it will be required to work (even though it does just fine today).

提交回复
热议问题