473,398 Members | 2,525 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

how to access something efficiently ??

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
3 1548
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Jay Moore | last post by:
Greetings, all! I have a project for work, and I'm not sure how to efficiently do what I need to do. I'm hoping someone out there can help. Project is this: I'm creating a web-based...
49
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The...
6
by: StepUP | last post by:
I'm a long time developer in Access. Noticing the "Why do IT guys hate MS Access?" thread made me think of my recent experiences in experimenting with VB .Net, and specifically, the Datagrid...
11
by: Ignacio X. Domínguez | last post by:
Hi. I'm developing a desktop application that needs to store some data in a local file. Let's say for example that I want to have an address book with names and phone numbers in a file. I would...
70
by: lgbjr | last post by:
Hello All, I've been developing a VB.NET app that requires the use of a DB. Up to now, I've been using Access. It's a bit slow, but everything works. I'm at a point now where I need to decide if...
62
by: Ecohouse | last post by:
I was just wondering if there was any way to use a toolbar in Outlook 2002 in Access 2002? I want to create a custom toolbar in Access similar to the Calendar toolbar in Outlook. Any ideas?
24
by: Lauren Wilson | last post by:
OK, I'm confused. SOME folks here seem to be saying it IS possible to link to or otherwise access and manipulate MDB files stored on a web server (from the client) and others seem to be saying...
5
by: B1ackwater | last post by:
We've fooled around with Access a bit, but only using the single-user store-bought version. It seems to be a good database - versatile and infinitely programmable - and can apparently be used as a...
1
by: ken | last post by:
Hi, Lets say we have a form that displays time card entries based on the calendar control date on the form. So the user clicks on a date and the form filters the table where the time card entries...
16
by: JoeW | last post by:
I'm utilizing a database that I created within MS Access within a program I've created in VB.NET. I am using the VB front end to navigate the information, but want to be able to print a report,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.