Today, we will learn C++ Array Assignment. We can assign multiple values to C++ Arrays. There are many ways you can initialize a C++ array. You can create different datatypes of arrays in C++ e.g. string, int, etc are the two most common types of C++ arrays that are commonly used. Today, we will see how to initialize and assign values to C++ arrays. Let’s start.
Declaring Empty C++ Array
We can declare an empty array in C++ using square brackets and specifying the number of items.
string city[5];
int id[6];
Declaring and C++ Array Assignment
We can declare and initialize a C++ Array at the same time. To initialize a C++ Array while declaring, we can use curly brackets and then values inside curly brackets.
string city[5] = {"Agra", "Delhi", "Noida", "Gurgaon", "Banglore"};
Accessing the elements of the C++ Array
You can use square brackets to access the elements of the C++ Array. Inside square brackets, use the index number of the element you want to access. Indexing in C++ starts from 0.
cout << city[0];
// Outputs Agra
Looping a C++ Array
#include <iostream>
#include <string>
using namespace std;
int main() {
string city[5] = {"Agra", "Delhi", "Noida", "Gurgaon", "Banglore"};
for(int i = 0; i < 5; i++){
cout << city[i] << "\n";
}
return 0;
}
Output:

Also Read:
- Top 25 Pattern Programs in C++
- Currency Converter in C++
- SQLite | CRUD Operations in Python
- Number Guessing Game in C++
- Image background remover in Python
- C++ Project Structure
- Python | Check if a string is a palindrome or not without Recursion
- Python | Check if a number is an Armstrong Number or not using Recursion
- Python | Check if a number is an Armstrong Number or not without using Recursion
- Python | Shuffle a list using recursion
- Python | Shuffle a list without recursion
- Python | Implementing switch case using functions
- Python function to check whether a number is perfect or not
- Python | Find LCM using function
- Python | Find HCF using function
- Python | Convert the binary number to decimal without using library function
- Python | Create a basic operations calculator(+, -, /, and x), create a separate function for each operation
- Python | Detecting the number of local variables declared in a function
- Python | Making a chain of function decorators (bold italic underline etc)
- Python | Access function inside a function
- Event Management System Project in Python
- ATM machine program in C++
- Python | Create a function with a pass statement
- Python | Function to calculate the square root of a number
- Python | A function that calculates the power of a number
- Python | A function that accepts 2 integers and adds them and returns their sum
- Python | Function that takes a list of integers and returns the last integer
- Python | Return multiple values from a function
- Python function that takes a list and returns a new list with unique elements of the first list
- Python | Generate a random number in the range of 1 to 10 using the function