Single Dimensional Arrays
#include <stdafx.h>
#include <iostream>
#include <string>
using namespace std;
/* An array is a fixed-size sequenced collection of elements of the same data type. */
int main()
{
string myArray[10]; // declares an array myArray of 10 strings.
myArray[0] = "aa"; // initializes first element
myArray[1] = "bb"; // initializes second element
myArray[2] = "cc"; // initializes third element
cout<< "Element at index 0 of myArray: "+ myArray[0]<< endl;
cout<< "Element at index 1 of myArray: "+ myArray[1]<< endl;
cout<< "Element at index 2 of myArray: "+ myArray[2]<< endl;
//or we can also initialize an array as
string newArray[]={"dd","ee","ff"};
cout<< "Element at index 0 of newArray: "+ newArray[0]<< endl;
cout<< "Element at index 1 of newArray: "+ newArray[1]<< endl;
cout<< "Element at index 2 of newArray: "+ newArray[2]<< endl;
return 0;
}
Output:
Element at index 0 of myArray: aa
Element at index 1 of myArray: bb
Element at index 2 of myArray: cc
Element at index 0 of newArray: dd
Element at index 1 of newArray: ee
Element at index 2 of newArray: ff