C++ Array (C++11)

beginner
5 min

C++ Array (C++11)

Welcome to the world of C++ arrays! In this comprehensive guide, we'll explore the fascinating world of arrays in C++11. By the end of this lesson, you'll be able to create, manipulate, and understand arrays in your C++ projects.

Let's start by understanding what an array is.

šŸ’” Pro Tip: An array is a collection of variables of the same data type, stored in contiguous memory locations.

Creating Arrays

To create an array, we first need to specify its data type and size. Here's a simple example:

cpp
#include <iostream> int main() { int myArray[5]; // Creating an array of 5 integers return 0; }

In the example above, we've created an array called myArray that can store 5 integers.

šŸ“ Note: The size of the array is fixed at the time of creation.

Accessing Array Elements

To access an array element, we use its index. The first element in an array has an index of 0.

cpp
#include <iostream> int main() { int myArray[5] = {1, 2, 3, 4, 5}; // Initializing an array std::cout << "The first element is: " << myArray[0] << std::endl; // Output: The first element is: 1 return 0; }

šŸ’” Pro Tip: Always remember that array indices start from 0.

Array Types

In C++, there are three types of arrays:

  1. One-dimensional arrays: These are the most common type of arrays. We've been working with one-dimensional arrays so far.

  2. Two-dimensional arrays: These are used to store data in a table-like structure, with rows and columns.

  3. Multi-dimensional arrays: These are arrays with more than two dimensions. They are less common but can be useful for specific scenarios.

Manipulating Arrays

We can manipulate arrays by performing various operations such as sorting, searching, and resizing.

Sorting Arrays

We can sort an array using built-in functions like std::sort().

cpp
#include <iostream> #include <algorithm> int main() { int myArray[] = {5, 3, 1, 4, 2}; std::sort(myArray, myArray + 5); // Sorting the array for(int i = 0; i < 5; i++) { std::cout << myArray[i] << " "; // Output: 1 2 3 4 5 } return 0; }

Searching Arrays

We can search an array for a specific value using a loop or a built-in function like std::find().

cpp
#include <iostream> #include <vector> #include <algorithm> int main() { int myArray[] = {5, 3, 1, 4, 2}; int target = 3; auto it = std::find(myArray, myArray + 5, target); // Finding the target in the array if(it != myArray + 5) { // If the target is found std::cout << "The target is found at position: " << std::distance(myArray, it) << std::endl; } else { std::cout << "The target is not found." << std::endl; } return 0; }

Quiz

Quick Quiz
Question 1 of 1

Which of the following is a valid one-dimensional array declaration in C++?

Conclusion

We've covered the basics of arrays in C++11 and learned how to create, manipulate, and sort arrays. In the next lesson, we'll dive deeper into arrays, including multi-dimensional arrays and other advanced topics.

Stay tuned and happy coding! šŸŽÆ