Solved

multidimensional array

Posted on 2014-03-17
4
219 Views
Last Modified: 2014-04-04
Hi,

I need help to create multidimensional array in c++. I need to store unknown records with 3fields. I hope, my choice for multidimensional array is correct. I want to store fields like

SupplierID | Withdraw | Deposit
..               |        ..        |
..               |       ..         |
..               |        ..        |


Thanks
0
Comment
Question by:josephm67
4 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 39934927
A std::vector of struct seems like a more flexible approach to me, e.g.

#include <vector>
#include <string>
using namespace std;

struct Record {

  string SupplierID;
  double Withdraw;
  double Deposit;
};

vector<Record> vRecords;

for (int i = 0; i < 10; ++i) {

  Record rc;

  rc.SupplierID = "John Doe";
  rc.Deposit = 100.0;
  rc.Withdraw = 42.0;

  vRecords.push_back(rc);
}

for (i = 0; i < 10; ++i) {

  Record rc = vRecord[i];

  // ...
}

Open in new window

0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 39934943
Do you want C++.net or C++ ?
They are NOT the same.  (The code from jkr is C++, with .net you have other built in options you could use).
0
 
LVL 33

Expert Comment

by:sarabande
ID: 39937065
you could implement a multidimensional array with vector as well.

class StringTable
{
     std::vector<std::vector<std::string> > arr2d;
public:
     StringTable(int nrows, int ncols) : arr2d(nrows, std::vector<std::string>(ncols)) {}
     std::vector<std::string> & operator[](int idx) { return arr2d[idx]; }
     int  getNumCols() { return (int)arr2d[0].size(); }
     int  getNumRows() { return (int)arr2d.size(); }
};    

int main ()
{
   StringTable myarr(5, 10);
   myarr[1][2] = "abc";
   if (myarr[0][2].empty() == false)
   {
         double withdraw = atof(myarr[0][2].c_str());
         ...
   }
   
   return 0;
}

Open in new window


note, for your problem the solution jkr has posted probably is better as it can use different types for columns. if the cells of your "table" may have mixed data or the column order might change, a string table like the one above could be the better way.

Sara
0
 

Author Closing Comment

by:josephm67
ID: 39978275
thanks
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
White board coding practice 3 92
Embarcadero WebBroker REST server endpoint parameters 2 82
Precision Problem in C++ 7 44
Android development question 2 54
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question