C++ Arrays
π¦ C++ Arrays
An array in C++ is a collection of multiple values of the same data type stored in contiguous memory locations and accessed using an index.
πΉ 1. Array Declaration
Example:
πΉ 2. Array Initialization
Method 1: Initialize at Declaration
Method 2: Size Automatically Determined
πΉ 3. Access Array Elements
π Index starts from 0.
πΉ 4. Modify Array Elements
πΉ 5. Array with for Loop
πΉ 6. Array User Input
πΉ 7. Range-Based for Loop (foreach)
πΉ 8. Find Array Length
πΉ 9. Multidimensional Array (2D Array)
Access:
πΉ 10. Common Array Operations
Sum of Elements
Find Maximum
β Common Mistakes
β οΈ Leads to undefined behavior.
π Array vs Vector
| Array | Vector |
|---|---|
| Fixed size | Dynamic size |
| Faster | Slightly slower |
| No bounds checking | Safer |
π Summary
-
Arrays store multiple values of the same type
-
Index starts from 0
-
Fixed size
-
Use loops to process elements
