12

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

1
  • 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? If so, have you consulted it? If not, you should get one. Commented Jan 6, 2011 at 6:25

6 Answers 6

6

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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!

2 Comments

do output.close() at the end.
No need; when the stream object goes out of scope the destructor will close the file.
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;
  }

Comments

1

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!";

Comments

-7

This will create a 2D string object of string:

String str[no of strings];

1 Comment

No, it will create a 1D array of strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.