[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
Paul Allen » CppCin » BeginnersGuideToCPlusPlus » CppDouble » ComputerScience » talk:OrphanPages » WhatLinksHere » node » WindowsFAQ » handle » CppArray
Prefer a std::vector over an array by default [1,2,3,5].
Static arrays cannot have a size of zero elements. This is used for compile-time assertions.
Prefer a std::vector to dynamically allocated arrays [1,2,3,4,5].
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
Paul Allen » CppCin » BeginnersGuideToCPlusPlus » CppDouble » ComputerScience » talk:OrphanPages » WhatLinksHere » node » WindowsFAQ » handle » CppArray
(C++) Array
A way to store a a large number of values and access them by index. There are two kinds of arrays:- Static arrays (size known at compile-time)
- Dynamically allocated arrays (size known at run-time)
Static arrays
//Create an array int array[10]; //Initialize it with non-random values for (int i=0; i!=10; ++i) array[i]=i; //Square them for (int i=0; i!=10; ++i) array[i]*=array[i]; //std::cout them for (int i=0; i!=10; ++i) std::cout << array[i] << std::endl;
Prefer a std::vector over an array by default [1,2,3,5].
Static arrays cannot have a size of zero elements. This is used for compile-time assertions.
Tip
The Boost C++ Library supplies a class called boost::array, which might be chosen over a std::vector when there is no need of resizing at run-time.How to determine a static array's size?
This can be done from the array's pointer and the size of the zero-th element:int main() { const int staticSize = 100; int myArray[staticSize]; const int size = (sizeof(myArray)/sizeof(myArray[0])); std::cout << "staticSize: " << staticSize << std::endl; std::cout << "size: " << size << std::endl; assert(size==staticSize); }
- include <iostream>
- include <cassert>
Dynamically allocated arrays
These array's sizes are unknown at compile-time and use new and delete[].int main() { const int randomSize = std::rand()%100; int * myArray = new int[randomSize]; delete[] myArray; }
- include <iostream>
- include <cassert>
Prefer a std::vector to dynamically allocated arrays [1,2,3,4,5].
How to determine a dynamically allocated array's size?
This can NOT[/red] be done from the array's pointer and the size of the zero-th element:int main() { const int randomSize = 2 + std::rand()%100; //Can never be 1 int * myArray = new int[randomSize]; const int size = (sizeof(myArray)/sizeof(myArray[0])); std::cout << "randomSize: " << randomSize << std::endl; std::cout << "size: " << size << std::endl; assert(size==1); assert(size!=randomSize); delete[] myArray; }
- include <iostream>
- include <cassert>
'Array' links
Code links
References
- 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) Scott Meyers. Effective STL. ISBN: 0-201-74962-9. Item 13: 'Prefer vector and string over dynamically allocated arrays'.
- 5) Bjarne Stroustrup. The C++ Programming Language (3rd edition).ISBN: 0-201-88954-4 Chapter C.14.11 'Prefer vector over array'.
- 6) 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'.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
