How to let client using [i][j] access two dimension array?
-
I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:
CMyResultSet res;
GetResultSet(res);
for (int i = 0; i < res.rows(); ++i)
{
string res1 = res[i][0].tostring();
}I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!
-
I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:
CMyResultSet res;
GetResultSet(res);
for (int i = 0; i < res.rows(); ++i)
{
string res1 = res[i][0].tostring();
}I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!
I went through internet and the most common suggestion i got is to use (int,int) instead of searching for [int] [int]; follow the link for example http://www.parashift.com/c++-faq-lite/operator-overloading.html#faq-13.8
-
I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:
CMyResultSet res;
GetResultSet(res);
for (int i = 0; i < res.rows(); ++i)
{
string res1 = res[i][0].tostring();
}I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!
I have tried it in this way:
#include
#include
#include
using namespace std;class CMyResultSet
{
public:
CMyResultSet(){}
~CMyResultSet(){}string GetResult(unsigned int i, unsigned int j) { return m\_Data\[i\]\[j\]; } void SetResult(string Result, unsigned int i) { if( i >= m\_Data.size()) { // New push to 1-dimension vector vec; vec.push\_back(Result); m\_Data.push\_back(vec); } else { // Push to an existing 1-dimension's 2nd dimension m\_Data\[i\].push\_back(Result); } }
private:
vector< vector > m\_Data;
};
void main()
{
CMyResultSet objResultSet;objResultSet.SetResult("00",0); objResultSet.SetResult("01",0); objResultSet.SetResult("10",1); objResultSet.SetResult("11",1); objResultSet.SetResult("20",2); cout<<"Value at 00:"<
-
I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:
CMyResultSet res;
GetResultSet(res);
for (int i = 0; i < res.rows(); ++i)
{
string res1 = res[i][0].tostring();
}I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!
-
I need to implement a class like a CMyResultSet class. The client code which call CMyResultSet would look like this:
CMyResultSet res;
GetResultSet(res);
for (int i = 0; i < res.rows(); ++i)
{
string res1 = res[i][0].tostring();
}I have no idea how to implement [][] to let the client code calling like that. How would the method's prototype look like? And How can I save the searched result set in two dimension array, using std::vector or what? Thanks in advance!
Does the problem you're working on particularly need 2 dimensional addressing like that? Quite often you'll find that in a table each column has different semantics and each row represents another entity with the same type of attributes as the one immediately above and below. So instead of using a vector of vectors or (shudder) a raw 2D array look to see if what you've is actually a simple, 1D, array of structures. You'll find that's particularly true if you're using a database. e.g. if instead of having:
string table[5][1024];
you might find something like this represents what your app is up to a bit more.
struct thingy
{
std::string name_;
std::string favourite_blanchmange_manufacturer_;
std::string why_they_want_world_peace_;
std::string smurf_theyd_most_like_to_stangle_;
std::string this_will_never_be_used_youll_just_have_to_initialise_it_forever_;
};thingy thingies[ 1024 ];
If you still need a 2D array come back and I'll try and add something else in here about it (but it won't be for a while). Cheers, Ash