简体   繁体   English

如何使用矢量<vector<string> &gt; 在 C++ 中?

[英]How to use vector<vector<string>> in c++?

I saw in c++ code the following:我在 C++ 代码中看到以下内容:

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 ?谁能解释它是什么以及如何使用 var arr

This is definition of 2 dimension-array of strings with size n.这是大小为 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] << " ";
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM