devil_lived
devil_lived

Reputation: 53

How to use vector<vector<string>> in c++?

I saw in c++ code the following:

vector<vector<string>> arr(n);

I was unable to understand how to use it...

Can anyone explain what is it and how to use var arr?

Upvotes: 2

Views: 13430

Answers (1)

dWinder
dWinder

Reputation: 11642

This is definition of 2 dimension-array of strings with size n.

You can use all places in the upper vector as another string vector.

Look at the following example:

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
  string a = "AAAA";
  string b = "BBBB";
  string c = "CCCC";
  int n = 3;
  vector<vector<string>> arr(n);

  arr[0].push_back(a); // I add string 'a' to end of first vector in 'arr' 
  arr[0].push_back(b);
  arr[1].push_back(c);
  for (int i = 0; i < arr[0].size() ; i++) { // print all string in first vector of 'arr'
     cout << arr[0][i] << " ";
  }
} 

Upvotes: 6

Related Questions