Array Initialization: Difference between revisions

From Rosetta Code
Content added Content deleted
m (Moved to Basic learning cat)
(Added C++ example)
Line 24: Line 24:
</Ada>
</Ada>
Note that the array bounds, when unconstrained as in these examples can be either determined by the aggregate, like the initialization of X shows. Or else they can be specified as a constraint, like for example in the initialization of Y. In this case '''others''' choice can be used to specify all unmentioned elements. But in any case, the compiler verifies that all array elements are initialized by the aggregate. Single dimensional arrays of characters can be initialized by character strings, as the variable S shows. Of course, array aggregates can participate in array expressions and these expressions can be used to initialize arrays. The variable B is initialized by an aggregate inversed by the operation '''not'''.
Note that the array bounds, when unconstrained as in these examples can be either determined by the aggregate, like the initialization of X shows. Or else they can be specified as a constraint, like for example in the initialization of Y. In this case '''others''' choice can be used to specify all unmentioned elements. But in any case, the compiler verifies that all array elements are initialized by the aggregate. Single dimensional arrays of characters can be initialized by character strings, as the variable S shows. Of course, array aggregates can participate in array expressions and these expressions can be used to initialize arrays. The variable B is initialized by an aggregate inversed by the operation '''not'''.

=={{header|C++}}==

Simple arrays in C++ have no bounds-checking. In order to safely modify the array's contents, you must know in advance both the number of dimensions in the array and the range for each dimension. In C++, all arrays are zero-indexed, meaning the first element in the array is at index 0.

To assign a single value to an array, one uses the [] operator.
// Assign the value 7 to the first element of myArray.
myArray[0] = 7;
If '''myArray''' has ten elements, one can use a loop to fill it.
// Assign the sequence 1..10 to myArray
for(int i = 0; i < 10; ++i)
myArray[i] = i + 1;
If '''myArray''' has two dimensions, you can use nested loops.
// Create a multiplication table
for(int y = 0; y < 10; ++y)
for(int x = 0; x < 10; ++x)
myArray[x][y] = (x+1) * (y+1);

===STL===
{{libheader|STL}}STL provides '''std::vector''', which behaves as a dynamically-resizable array. When an element is added, its value must be set immediately.
myVector.push_back(value);
Like simple arrays, '''std::vector''' allows the use of the [] operator, and once an element has been added, it can be changed the same way a simple array can.
myVector[0] = value;
Unlike simple arrays, '''std::vector''' allows you to determing the size of the array. You can use this set all of the values in the array:
// Create a list of numbers from 1 to the size of the vector.
size_t size = myVector.size();
for(int i = 0; i < size; ++i)
myVector[i] = i + 1;
'''std::vector''' also provides iterators, allowing you to iterate through a vector's elements the same way you might any other STL container class.
// Create a list of numbers from 1 to the size of the vector.
std::vector<int> myVector;
int val = 0;
for(std::vector<int>::iterator it = myVector.begin();
it != myVector.end();
++it)
*it = ++val;

Revision as of 23:08, 12 October 2008

Task
Array Initialization
You are encouraged to solve this task according to the task description, using any language you may know.

Demonstrate how to initialize an array variable with data.

See Creating_an_Array for this topic.

Ada

The array value obtained directly from data is called array aggregate. Considering these array declarations: <Ada> type Vector is array (Integer range <>) of Integer; type Matrix is array (Integer range <>, Integer range <>) of Integer; type String is array (Integer range <>) of Character; type Bits is array (Integer range <>) of Boolean; </Ada> Initialization by an aggregate using positional and keyed notations: <Ada> X : Vector := (1, 4, 5); Y : Vector (1..100) := (2|3 => 1, 5..20 => 2, others => 0); E : Matrix := ((1, 0), (0, 1)); Z : Matrix (1..20, 1..30) := (others => (others => 0)); S : String := "ABCD"; L : String (1..80) := (others => ' '); B : Bits := not (1..2 => False); -- Same as (1..2 => True) </Ada> Note that the array bounds, when unconstrained as in these examples can be either determined by the aggregate, like the initialization of X shows. Or else they can be specified as a constraint, like for example in the initialization of Y. In this case others choice can be used to specify all unmentioned elements. But in any case, the compiler verifies that all array elements are initialized by the aggregate. Single dimensional arrays of characters can be initialized by character strings, as the variable S shows. Of course, array aggregates can participate in array expressions and these expressions can be used to initialize arrays. The variable B is initialized by an aggregate inversed by the operation not.

C++

Simple arrays in C++ have no bounds-checking. In order to safely modify the array's contents, you must know in advance both the number of dimensions in the array and the range for each dimension. In C++, all arrays are zero-indexed, meaning the first element in the array is at index 0.

To assign a single value to an array, one uses the [] operator.

 // Assign the value 7 to the first element of myArray.
 myArray[0] = 7;

If myArray has ten elements, one can use a loop to fill it.

 // Assign the sequence 1..10 to myArray
 for(int i = 0; i < 10; ++i)
   myArray[i] = i + 1;

If myArray has two dimensions, you can use nested loops.

 // Create a multiplication table
 for(int y = 0; y < 10; ++y)
   for(int x = 0; x < 10; ++x)
     myArray[x][y] = (x+1) * (y+1);

STL

Library: STL

STL provides std::vector, which behaves as a dynamically-resizable array. When an element is added, its value must be set immediately.

 myVector.push_back(value);

Like simple arrays, std::vector allows the use of the [] operator, and once an element has been added, it can be changed the same way a simple array can.

 myVector[0] = value;

Unlike simple arrays, std::vector allows you to determing the size of the array. You can use this set all of the values in the array:

 // Create a list of numbers from 1 to the size of the vector.
 size_t size = myVector.size();
 for(int i = 0; i < size; ++i)
   myVector[i] = i + 1;

std::vector also provides iterators, allowing you to iterate through a vector's elements the same way you might any other STL container class.

 // Create a list of numbers from 1 to the size of the vector.
 std::vector<int> myVector;
 int val = 0;
 for(std::vector<int>::iterator it = myVector.begin();
     it != myVector.end();
     ++it)
   *it = ++val;