Solved

multidimensional array

Posted on 2014-03-17
4
224 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 34

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
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.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

734 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