C++ Array of Strings
There are a few different ways of creating a c++ Arrays of Strings
#include <array>
using namespace std;
std::array<string,4> family = {“brother”,”sister”,”father”,”mom”}
// std::array usage std::array<string,3> family = {"Me","You","Them"} #include <iostream> #include <array> using namespace std; int main(){ std::array<string,6> classmates = {"George", "Amy","John","Joanne","Stevens","Anonymous"}; for ( unsigned int i = 0; i < classmates.size();i++) std::cout << "My classmate is: " << classmates[i] << std::endl; }
Grab the code above and copy paste it into your compiler/editor. Run it yourself and see what it produces
In this Array of Strings Code example, lines 5, 7 and 8 deserve some attention
Line 3 is #include <array> which is new. Get used to it. It’s legitimate.
Line 5 is using namespace std; without this line, we would have needed to write std:array<std::string,5> which was going to look a bit messy.
Line 7 is special. std::array<string,5> classmates is workable and legitimate and very c++ ‘ish. However in this same tutorial we have set up examples that were simpler, such as this example below.
string classmates[4] = {“George”, “Amy”,”John”,”Joanne”,”Stevens”};
Totally legitimate and it works in both C and C++
Line 8 also introduces 2 more new elements unsigned int and std::array::size. unsigned int means there can be no negatives and therefore int has a larger possibility of positive numbers. Unlike the language operator sizeof, which returns the size in bytes, this member function std::array:size returns the size of the array in terms of the number of elements.

Homework — Yes, More of that fantastic stuff
- 1) Memorize this short code
- 2) Rewrite the code above into your c++ compiler/editor
- 3) Change the number of class mates to just 4
- 4) Change their names
Next Lesson 9,Math Functions C++