[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

multidimensional array

Posted on 2014-03-17
4
Medium Priority
?
230 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 2000 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 45

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 35

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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 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.
Suggested Courses

656 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