Solved

multidimensional array

Posted on 2014-03-17
4
197 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 32

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now