array is one of these topics:
- A plain array
- std::tr1::array
- boost::array
- std::array
See array/std::array/boost::array example 1: comparison for a comparison.
A plain array
An array is a collection of elements that can be accessed by the index operator.
int my_array[10]; //Create an array that stores ten integers
Prefer a std::vector (or perhaps std::array) over an array by default [1-4]. Consider not using arrays in the interface of a class.
The first element of an array is at index zero.
There are two kinds of arrays:
-
- Static arrays: size known at compile-time, for example 'int v[10]'
-
- Dynamically allocated arrays: size gets determined at run-time, for example 'int * v')
- Take care not to write beyond the bounds of an array [5,7]
- Avoid multidimensional arrays; define suitable containers instead [8]
- Use containers rather than plain arrays [9]
- Use std::string rather than zero-terminated arrays of chars [10]
- Avoid passing arrays as pointers [11]
- Use std::vector and std::string instead of arrays [15] (note: this was before std::array was in the STL)
- Prefer std::array to built-in arrays [12-14,16]
- Array example 1: create different array types (static, dynamic, std::array, boost::array)
- Array example 2: array of Derived falls back to an array of Base without warnings
- Array example 3: passing an array as a pointer and size cannot be checked to be correct
- Array example 4: benchmark comparing speed of a plain array with a std::vector
- Array example 5: benchmark comparing speed of a plain array with a std::array
- Array example 6: passing an array as T* or T[] is equivalent
- Array example 7: creating an array of negative size is accepted without warning
- Array example 8: an array is not a regular type
- [1] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 Chapter 5.8.4 'Use vector and valarray rather than built-in (C-style) arrays'
- [2] Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 76: 'Use vector by default. Otherwise choose an appropriate container'
- [3] Marshall Cline, Greg Lomow and Mike Girou. C++ FAQs. ISBN: 0-201-3098301, FAQ 28.02: 'Are arrays good or evil?' (Answer: 'Arrays are evil'
- [4] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 Chapter C.14.11 'Prefer vector over array'
- [5] Bjarne Stroustrup. The C++ Programming Language (3rd edition). ISBN: 0-201-88954-4 5.8.2: 'Take care not to write beyond the bounds of an array'
- [6] Joint Strike Fighter Air Vehicle C++ Coding Standards for the System Development and Demonstration Program. Document Number 2RDU00001 Rev C. December 2005. AV Rule 97: 'Arrays shall not be used in interfaces. Instead, the Array class should be used.'
- [7] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[3] Take care not to write beyond the bounds of an array'
- [8] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[4] Avoid multidimensional arrays; define suitable containers instead'
- [9] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[6] Use containers (e.g., vector, array, and valarray) rather than built-in (C-style) arrays'
- [10] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 7.8. Advice. page 199: '[7] Use string rather than zero-terminated arrays of chars'
- [11] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 12.7. Advice. page 341: '[15] Avoid passing arrays as pointers'
- [12] Scott Meyers. C++ And Beyond 2012 session: 'Initial thoughts on Effective C++11'. 2012. 'Prefer std::array to Built-in Arrays'
- [13] Scott Meyers. Effective Modern C++ (1st Edition). 2014. ISBN: 978-1-491-90399-5. Item 1, page 17: 'Of course, as a modern C++ developer, you'd naturally preder a std::array to a built-in array'
- [14] Bjarne Stroustrup. A tour of C++. 2014. ISBN: 978-0-321-958310. Chapter 11.7.11: 'Prefer array over built-in arrays'
- [15] Herb Sutter and Andrei Alexandrescu. C++ coding standards: 101 rules, guidelines, and best practices. ISBN: 0-32-111358-6. Chapter 77: 'Use vector and string instead of arrays'
- [16] Bjarne Stroustrup. The C++ Programming Language (4th edition). 2013. ISBN: 978-0-321-56384-2. Chapter 34.7. Advice. page 1007: '[2] Prefer array over built-in arrays'