Home Arrays with C++
Post
Cancel

Arrays with C++ (C/C++).

Arrays with C++


In programming, an array is a collection of elements of the same data type, stored in contiguous memory locations. Arrays are a fundamental data structure in programming, and understanding how to use them is essential for writing effective code.

Declaring Arrays in C++


To declare an array in C++, you use square brackets to specify the size of the array and braces to initialize its values:

1
int myArray[5] = {1, 2, 3, 4, 5};

You can also declare an array without initializing its values, like this:

1
int myArray[5];

Accessing Array Elements


Once you’ve declared an array, you can access its elements using their index. Array indices in C++ start at 0, so the first element of an array is at index 0, the second element is at index 1, and so on.

1
2
3
int myArray[5] = {1, 2, 3, 4, 5};
cout << myArray[0] << endl; // prints 1
cout << myArray[2] << endl; // prints 3

You can also use a loop to iterate over the elements of an array:

1
2
3
4
int myArray[5] = {1, 2, 3, 4, 5};
for (int i = 0; i < 5; i++) {
  cout << myArray[i] << endl;
}

This loop will print out each element of the array on a separate line.

Multidimensional Arrays


Arrays can also be multidimensional, meaning they have more than one dimension. To declare a two-dimensional array in C++, you use two sets of square brackets:

1
2
3
int myArray[3][3] = {
  {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
};

To access an element of a two-dimensional array, you use two indices:

1
2
3
4
5
int myArray[3][3] = {
  {1, 2, 3}, {4, 5, 6}, {7, 8, 9}
}; 
cout << myArray[0][0] << endl; // prints 1
cout << myArray[1][2] << endl; // prints 6

Common Operations on Arrays


There are several common operations that can be performed on arrays in C++, such as sorting and searching. The algorithm library provides functions for these operations:

1
2
3
4
5
6
7
8
9
10
11
int myArray[5] = {5, 3, 1, 4, 2};
// sort the array in ascending order
sort(myArray, myArray + 5);

// search for the value 3 in the array 
int* result = find(myArray, myArray + 5, 3);
if (result != myArray + 5) { 
  cout << "Found at index " << result - myArray << endl;
} else {
  cout << "Not found" << endl;
}

This code will sort the array in ascending order and then search for the value 3 in the array. If the value is found, it will print out its index.

Conclusion


And that’s a brief overview of arrays in C++. Arrays are a fundamental data structure in programming, and understanding how to use them is essential for writing effective code. With arrays, you can store and access large amounts of data quickly and efficiently.

This post is licensed under CC BY 4.0 by the author.

Vectors with C++

-