簡體   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