typedef std::vector<std::string> StringVector;
typedef std::vector<StringVector> StringVector2D;
StringVector2D twoD;
for (StringVector2D::iterator outer = twoD.begin(); outer != twoD.end(); ++outer)
for (StringVector::iterator inner = outer->begin(); inner != outer->end(); ++inner)
std::cout << *inner << std::endl;
Run Code Online (Sandbox Code Playgroud)
小智 5
一起声明和初始化:
std::string myarray[2][3] = {
{ "hello", "jack", "dawson" },
{ "hello", "hello", "hello" }
};
Run Code Online (Sandbox Code Playgroud)
对于写入文件,templatetypedef 的答案几乎没问题,除了您应该进行错误检查并在完成后关闭输出文件流。
您可以像这样声明一个多维字符串数组:
std::string myArray[137][42];
Run Code Online (Sandbox Code Playgroud)
当然,用您自己的宽度/高度值替换 137 和 42。:-)
没有“一种正确的方法”将该数组写入磁盘。您实际上将使用某种适当的分隔符和错误检查逻辑来迭代数组,一次将一个字符串写入磁盘。这是一种简单的实现,它每行写出一个字符串(假设字符串中没有任何换行符):
std::ofstream output("result.txt");
for (size_t i = 0; i < 137; ++i)
for (size_t j = 0; j < 42; ++j)
output << myArray[i][j] << std::endl;
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!