Solved

multidimensional array

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

816 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

11 Experts available now in Live!

Get 1:1 Help Now