0

I have an array:

const string ARRAY[][3] = {
    {"Kolkata","Mumbai","218"},
    {"Kolkata","New Delhi","316"},
    ...
    {"Mumbai","Chennai","715"},
    {"Chennai","Bangalore","516"},
};

This was a convenient way to store the entire table because I knew how many rows I had. However, in the future, the data needs to be read from a file, which can and will be edited by anyone.

I thought of creating a struct, something like:

typedef struct row {
    string col1;
    string col2;
    string col3;
} row_t;

and then creating a vector<row_t>. Is this a good idea? Is there an easier way of doing this using an stl container? I also thought of creating a table large enough to last for a while (the number of rows does not change that often), but that does not feel right..

Thanks!

Sagar
  • 9,456
  • 6
  • 54
  • 96
  • Your default constructor is unneccessary (it doesn't do anything that the implicitly defined default constructor will do). Names beginning with underscores are in many cases reserved to the implementation (there are rather [complex rules](http://stackoverflow.com/questions/228783/) specifying when you are allowed to use them and when you are not allowed to use them). It's best just to avoid prefixing names with underscores. – James McNellis Jan 19 '11 at 20:50

4 Answers4

2

The most straightforward conversion would be to use an array in the struct:

struct row {
    std::string data[3];
};

std::vector<row> v;

Or you can use the std::/std::tr1::/boost:: array class template:

std::vector<std::array<std::string, 3> > v;

If you are going to use separate data members for each of the elements, as you show in your example, you should give them useful names and not just name them "col#".

James McNellis
  • 348,265
  • 75
  • 913
  • 977
  • Unfortunately, boost is not an option. I did look in to it, though, and that would have made it easier. – Sagar Jan 19 '11 at 20:55
  • @Sagar: That class template is all of 100 lines of code at most. You could easily extract it from Boost and/or implement your own version with the same interface very easily. – James McNellis Jan 19 '11 at 20:57
1

That's a pretty fine idea. You just need to provide an operator>> to read from file and you're done. However, I genuinely don't understand the typedef struct{} stuff you have going on there- C++ has never required that.

Puppy
  • 144,682
  • 38
  • 256
  • 465
  • Reading from the file is not an issue at all. As for the struct, it was just something to make it easier to handle the rows. I'm not sure what you mean by "C++ has never required that".. – Sagar Jan 19 '11 at 20:51
  • @Sagar: It's a hangover from C that is essentially unnecessary. `struct row { ... };` is sufficient to define a type called `row`. – Oliver Charlesworth Jan 19 '11 at 21:06
0

This approach is fine, assuming the number of columns is never going to change. You could create a vector of vectors, or modify your struct to contain a vector.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

You could very likely get away with aggregating vectors.

vector < vector < string > > Array;

You could then resize either rows / columns as needed. If the struct is more convenient for you because of the fixed number of entries per row, you could easily use your suggestion.

vector < row_t > Array;
James
  • 5,355
  • 2
  • 18
  • 30