Use of pointers and arrays

Jul 3, 2013 at 8:28pm
Hello guys I'm a a beginner programmer that is currently reading C++ Primer 4th Edition. I have just finished learning about iterators, vectors, arrays and pointers. In the book it says that arrays and pointers are error prone, and that most modern programs use vectors and iterators over arrays and pointers. So my question is; is this true? Also, in what situations are arrays and pointers more benefical that vectors?

Jul 3, 2013 at 8:55pm
Afaik, this is true as vectors allow you to easily resize the "array", and by using iterators, we can "iterate" even over a non-linear container:

1
2
3
4
5
std::set<int> numbers {1, 2, 3, 4, 8, 9, 12, 9, 9, 0, -32, -64, 64};
for (std::set<int>::iterator it = numbers.begin(); it != numbers.end(); ++it)
{
    std::cout << *it << std::endl;
}


Arrays and pointers are more beneficial when you want to get close to the metal. Afaik std::vector<T> uses an std::size_t to store the size of the vector, so that uses some memory. Pointers are useful when you want to "point" to objects. They're especially useful since removing the pointer itself will not remove the data pointed to in memory.
Jul 3, 2013 at 9:30pm
So my question is; is this true?

Yes, although there are many more containers besides vectors, too.

Also, in what situations are arrays and pointers more benefical that vectors?

I've used arrays (C++ arrays mostly, but 4th edition doesn't cover those) to hold data known at compile time. Also, arrays have the occasionally useful property that they do not need an allocator. It's pretty rare overall. Pointers have many other purposes besides serving as iterators in arrays, as already pointed out.
Last edited on Jul 3, 2013 at 9:34pm
Jul 4, 2013 at 5:43pm
Thank you guys for your input.
Topic archived. No new replies allowed.