I have an std::array<std::string, 4>
that I want to use to hold path names for text files that I want to read into the program. So far, I declare the array, and then I assign each std::string
object the value.
#include <array>
#include <string>
using std::array;
using std::string;
...
array<string, 4> path_names;
path_names.at(0) = "../data/book.txt";
path_names.at(0) = "../data/film.txt";
path_names.at(0) = "../data/video.txt";
path_names.at(0) = "../data/periodic.txt";
I am using this approach because I know in advance that there are exactly 4 elements in the array, the size of the array will not change, and each path name is hard coded and also cannot change.
I want to declare and initialize the array and all its elements in just one step. For example, this is how I would declare an initialize an array of int
s:
array<int, 10> ia2 = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; // list initialization
How would I do the same but for std::string
? Creating these strings would involve calling the constructor. It's not as simple as just list-initializing a bunch of int
s. How to do that?