繁体   English   中英

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

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

我在 C++ 代码中看到以下内容:

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

我无法理解如何使用它...

谁能解释它是什么以及如何使用 var arr

这是大小为 n 的字符串的二维数组的定义。

您可以将上向量中的所有位置用作另一个字符串向量。

看下面的例子:

#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