Skip to content
Permalink
Browse files
Added arrays.cpp even though Vector is better
  • Loading branch information
nagrat committed Mar 15, 2023
1 parent e451e94 commit 5239d328c991276e26d5a7c80b16aa587e8d2a58
Showing 1 changed file with 34 additions and 0 deletions.
@@ -0,0 +1,34 @@
#include <iostream>
#include <array>

using namespace std;

/*
Arrays < Vectors. Flexibility > Not Flexible
*/


int main() {
string myArray[3]; // define new array - data-type string, element size of 3
// assigning elements to index 0, 1, and 2
myArray[0] = "C++";
myArray[1] = "Has";
myArray[2] = "Arrays";
// printing out each element
cout << myArray[0] << " " << myArray[1] << " " << myArray[2] << endl;

for (const string& x: myArray) { // looping through the array
cout << x << " "; // printing out each element seperated by a space
}
cout << "\n";

// Making arrays a different way
// using the template format
array<int, 5> myOtherArray = {1, 2, 3, 4, 5};
// same process of getting elements, just don't need to define elements after as already done as such
for (const int& x : myOtherArray) { // looping through the array again
cout << x << " ";
}
cout << "\n";
}

0 comments on commit 5239d32

Please sign in to comment.