kbyun
kbyun

Reputation: 442

How to initialize a vector of vector of strings using for loop in C++?

I have problem with initializing the following vector:

int main()
{
    ...
    int size = classData.size();
    vector<vector<string>> arrayClass[size][3];     // <-- problem
    for(int i = 0 ; i < classData.size(); i++)
    {
        for(int j = 0 ; j < 3; j++)
        {
            arrayClass[i][j] = classData[j+i];
        }
    }
}

It says that size must be constant value. Any thoughts?

Upvotes: 3

Views: 3459

Answers (1)

LihO
LihO

Reputation: 42133

vector<vector<string>> arrayClass[size][3];

was meant to be:

vector<vector<string>> arrayClass(size, vector<string>(3));

which takes advantage od std::vector's constructor, which initializes the vector with appropriate size, filling it with empty strings.

Upvotes: 8

Related Questions