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:
- The system of the binary conversion
- What is web development for beginners?
- Guide to Proxy Servers: How They Work and Why You Need Them?
- Python | Check Armstrong Number using for loop
- Python | Factorial of a number using for loop
- Link in bio
- Microsoft Giving Free Machine Learning Course: Enroll Now
- Accenture Giving Free Developer Certificate in 2023
- Python | Asking the user for input until they give a valid response
- Python | How to iterate through two lists in parallel?
- Amazon Summer Internship 2023
- Python | How to sort a dictionary by value?
- Amazon Giving Free Machine Learning Course with Certificate: Enroll Now
- Google Summer Internship 2023
- Python | How to split a list into equally-sized chunks?
- 5 Secret ChatGPT skills to make money
- Python | Remove items from a list while iterating
- Free Google Certification Courses
- 5 AI tools for coders better than ChatGPT
- Python | How to get dictionary keys as a list
- New secrets to Earn money with Python in 2023
- Flower classification using CNN
- How to represent Enum in Python?
- 5 methods | Flatten a list of lists | Convert nested list into a single list in Python
- What does if __name__ == __main__ do in Python?
- Music Recommendation System in Machine Learning
- Brick Breaker Game in C++
- Dino Game in Java
- Java Games Code | Copy And Paste
- How to utilize ChatGPT to improve your coding skills?