C++ 初始化二维字符串数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4612328/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initializing a two dimensional array of strings
提问by honey
How to declare a two dimensional array of strings in c++? And also how to write this string on files?
如何在 C++ 中声明一个二维字符串数组?还有如何在文件上写这个字符串?
回答by honey
Declaration and initialization together:
一起声明和初始化:
std::string myarray[2][3] = {
{ "hello", "Hyman", "dawson" },
{ "hello", "hello", "hello" }
};
For writing to file, templatetypedef's answer is almost fine, except you should do error checking and close the output file stream when done.
对于写入文件,templatetypedef 的答案几乎没问题,除非您应该进行错误检查并在完成后关闭输出文件流。
回答by Mark Ransom
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;
回答by templatetypedef
You can declare a multidimensional array of strings like this:
您可以像这样声明一个多维字符串数组:
std::string myArray[137][42];
Of course, substituting your own width/height values for 137 and 42. :-)
当然,将您自己的宽度/高度值替换为 137 和 42。:-)
There's no "one right way" to write this array to disk. You'll essentially be iterating over the array writing one string at a time to disk, with some sort of appropriate separators and error-checking logic. Here's one naive implementation, which writes out one string per line (assuming that the strings don't have any newlines in them):
没有“一种正确的方法”可以将此数组写入磁盘。您基本上将迭代数组,一次将一个字符串写入磁盘,并使用某种适当的分隔符和错误检查逻辑。这是一个简单的实现,它每行写出一个字符串(假设字符串中没有任何换行符):
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;
Hope this helps!
希望这可以帮助!
回答by Manish Bhadani
#include<iostream>
#include<vector>
using namespace std;
main()
{
vector< vector<string> > m2m;
vector<string> A, B;
vector< vector<string> >::iterator inter_i;
vector<string>::iterator inter_j;
A.push_back("micro");
A.push_back("soft");
A.push_back("bilgates");
B.push_back("linux");
B.push_back("unix");
B.push_back("ken dennish");
m2m.push_back(A);
m2m.push_back(B);
cout<<endl<<" USing iterator : "<<endl;
for(inter_i=m2m.begin();inter_i!=m2m.end();inter_i++)
{
for(inter_j=(*inter_i).begin();inter_j!=(*inter_i).end();inter_j++)
{
cout<<*inter_j<<" ";
}
cout<<endl;
}
return 0;
}
回答by Arenim
I assume, that You've QString type. This should work to std::string and even (char*) properly.
我假设,你有 QString 类型。这应该适用于 std::string 甚至 (char*) 正确。
QString ** myTwoDimensionalArray;
myTwoDimensionalArray = new QString*[size_x];
for(int i=0; i<size_x; i++) myTwoDimensionalArray[i] = new QString[size_y];
That's it. Now, You can write something like:
就是这样。现在,您可以编写如下内容:
myTwoDimensionalArray[x][y] = "Hello, World!";