11

How to declare a two dimensional array of strings in c++? And also how to write this string on files?

jww
  • 97,681
  • 90
  • 411
  • 885
honey
  • 111
  • 1
  • 1
  • 3
  • 2
    What kind of strings? String literals? C strings? `std::string` objects? `CString` objects? `QString` objects? Unicode strings of some kind? Encrypted strings? Some other kind of strings? What kind of file do you need to write them to? Do they need to be encoded in a particular way in the file? Do you have [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c++-book-guide-and-list)? If so, have you consulted it? If not, you should get one. – James McNellis Jan 06 '11 at 06:25

6 Answers6

5
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;
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
5

Declaration and initialization together:

std::string myarray[2][3] = {
  { "hello", "jack", "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.

2
#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;
  }
Manish Bhadani
  • 437
  • 6
  • 13
2

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. :-)

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!

templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065
0

I assume, that You've QString type. This should work to std::string and even (char*) properly.

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!";
Arenim
  • 4,097
  • 3
  • 21
  • 31
-7

This will create a 2D string object of string:

String str[no of strings];
m7913d
  • 10,244
  • 7
  • 28
  • 56