One Dimensional Arrays
The data type array can be used to store a number of objects of an arbitrary type T such that the objects can be accessed uniquely by an index
of an integer interval.
Example
The following program shows how to use Arrays. It generates an array
A of 100 ints with indices from 1 to 100. Then it assigns value i
to A[i] . Finally, it accesses the elements with index 73 and
99.
#include <LEDA/core/array.h>
int main()
{
leda::array<int> A(1,100);
int i;
for (i=A.low(); i<=A.high(); i++) A[i]=i;
std::cout << A[73] << " " << A[99] << std::endl;
return 0;
}
Strengths
- access and insertion of objects by index in constant time
- induced order on objects
- index set can be arbitrary interval of integers (it does not have
to start at 0)
- Array bounds are checked
Disadvantages
- waste of space if Array is bigger than the current number of objects
(alternatives: Linear Lists, Sets)
- searching for an element is slow (proportional to the size of the
array) (alternative: Sets)
- initialization is necessary
Tips
- Use an Array if you know the number of elements to store (at least
approximately) beforehand and you need to access the elements efficiently
by an integer index.
- If the number of objects to store varies a lot, consider using a Linear
List or a Set
- If you need to search for objects frequently consider using a Set.
- If you need to iterate over the current elements frequently use a
Linear List
- If you need to insert/delete elements fast at an arbitrary position
use a Linear List
|
See also:
Linear Lists
Sets
Two Dimensional Arrays
Node Arrays
Edge Arrays
Face Arrays
Manual Entries:
Manual
Page One Dimensional Arrays
User
Defined Parameter Types
|