By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,433 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,433 IT Pros & Developers. It's quick & easy.

how to access something efficiently ??

P: n/a
hello
I have a problem and it is something like this .

I have 5 static arrays in one class called S1 ,S2 ....S5(all 2 D arrays).
Now , I want to access some number of elements in each one of the arrays .
But i don't want to explicitly specify the names of all the arrays ,
say S1[k][j] or S2[k][j] in order to do a similar look up .
What are the design ways in which I can get this done. ??
One possibility was that I could call a function with an argument like,

int foo(int row,int col ,int n){
switch(n)
{
case 1: return S1[row][col];break;
case 2: return S2[row][col];break;
...
}
}

another way is to make it a 3 D array ......
All this is for doing a table lookup ..can someone please suggest a better
way to do this , maybe using some design technique (say something with
function objects etc....??)
Thanking you,
Maadhuu.

Nov 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
maadhuu <ma************@yahoo.com> wrote:
hello
I have a problem and it is something like this .

I have 5 static arrays in one class called S1 ,S2 ....S5(all 2 D arrays).
Now , I want to access some number of elements in each one of the arrays .
But i don't want to explicitly specify the names of all the arrays ,
say S1[k][j] or S2[k][j] in order to do a similar look up .
What are the design ways in which I can get this done. ??
One possibility was that I could call a function with an argument like,

int foo(int row,int col ,int n){
switch(n)
{
case 1: return S1[row][col];break;
case 2: return S2[row][col];break;
..
}
}

another way is to make it a 3 D array ......
All this is for doing a table lookup ..can someone please suggest a better
way to do this , maybe using some design technique (say something with
function objects etc....??)
Thanking you,
Maadhuu.


Almost the same as the function foo(), you could overload operator().

http://www.parashift.com/c++-faq-lit...html#faq-13.10
may be helpful.

--
Marcus Kwok
Nov 22 '05 #2

P: n/a
"maadhuu" <ma************@yahoo.com> wrote in message
news:72******************************@localhost.ta lkaboutprogramming.com...
: hello
: I have a problem and it is something like this .
:
: I have 5 static arrays in one class called S1 ,S2 ....S5(all 2 D
arrays).
: Now , I want to access some number of elements in each one of the
arrays .
: But i don't want to explicitly specify the names of all the arrays ,
: say S1[k][j] or S2[k][j] in order to do a similar look up .
: What are the design ways in which I can get this done. ??
: One possibility was that I could call a function with an argument
like,
:
: int foo(int row,int col ,int n){
: switch(n)
: {
: case 1: return S1[row][col];break;
: case 2: return S2[row][col];break;
: ..
: }
: }
:
: another way is to make it a 3 D array ......
Yes. And what is wrong with the switch() option ?

: All this is for doing a table lookup ..can someone please suggest a
better
: way to do this , maybe using some design technique (say something
with
: function objects etc....??)
If you really want another approach, you could use something like:
typedef Data2D[N_ROWS][N_COLS];
Data2D S1 = {.......}; //etc
Data2D& sAllTables[5] = { S1, S2, S3, S4, S5 };
int foo(int row,int col ,int n){ return sAllTables[n][row][col]; }

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com
Nov 22 '05 #3

P: n/a
maadhuu wrote:
hello
I have a problem and it is something like this .

I have 5 static arrays in one class called S1 ,S2 ....S5(all 2 D arrays).
Now , I want to access some number of elements in each one of the arrays .
But i don't want to explicitly specify the names of all the arrays ,
say S1[k][j] or S2[k][j] in order to do a similar look up .
What are the design ways in which I can get this done. ??
One possibility was that I could call a function with an argument like,

int foo(int row,int col ,int n){
switch(n)
{
case 1: return S1[row][col];break;
case 2: return S2[row][col];break;
..
}
}

another way is to make it a 3 D array ......
All this is for doing a table lookup ..can someone please suggest a better
way to do this , maybe using some design technique (say something with
function objects etc....??)
Thanking you,
Maadhuu.


You could use a template member function. E.g.,

#include <iostream>
using namespace std;

class Table
{
public:
// No body
template<int i> int Get( int, int );

private:
static int S1[ 10 ][ 10 ];
static int S2[ 10 ][ 10 ];
static int S3[ 10 ][ 10 ];
static int S4[ 10 ][ 10 ];
static int S5[ 10 ][ 10 ];
};

// Specialize
template<> inline int Table::Get<1>(int r, int c) {return S1[r][c];}
template<> inline int Table::Get<2>(int r, int c) {return S2[r][c];}
template<> inline int Table::Get<3>(int r, int c) {return S3[r][c];}
template<> inline int Table::Get<4>(int r, int c) {return S4[r][c];}
template<> inline int Table::Get<5>(int r, int c) {return S5[r][c];}

int Table::S1[ 10 ][ 10 ];
int Table::S2[ 10 ][ 10 ];
int Table::S3[ 10 ][ 10 ];
int Table::S4[ 10 ][ 10 ];
int Table::S5[ 10 ][ 10 ];
int main()
{
table t;
//cout << t.Get<100>( 4, 3 ) << endl; // Compiler error!
cout << t.Get<1>( 4, 3 ) << endl;
return 0;
}

Nov 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.