Whats the difference? Pointer to an array vs regular array

后端 未结 9 1687
梦如初夏
梦如初夏 2021-01-04 21:40

I\'m familiar with Java and trying to teach myself C/C++. I\'m stealing some curriculum from a class that is hosting their materials here. I unfortunately can\'t ask the tea

9条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-04 22:13

    First, I'd look for some other place to learn C++. The page you cite is very confusing, and has little to do with the way one actually programs in C++. In C++, most of the time, you'd use std::vector for an array, not the complex solutions proposed on the page you cite. In practice, you never use operator new[] (an array new).

    In fact, std::vector is in some ways more like ArrayList than simple arrays in Java; unlike an array in Java, you can simply grow the vector by inserting elements into it, preferrably at the end. And it supports iterators, although C++ iterators are considerably different than Java iterators. On the other hand, you can access it using the [] operator, like a normal array.

    The arrays described on the page you cite are usually called C style arrays. In C++, their use is mostly limited to objects with static lifetime, although they do occasionally appear in classes. In any case, they are never allocated dynamically.

提交回复
热议问题