473,406 Members | 2,281 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,406 software developers and data experts.

STL Map; How to access a specific index?

Hi

I have a class which makes much use of a Map:

typedef map<char, string, less<char LSysRule;

As you can see, text strings are stored with characters as the key.

I'm trying to re-use the class in another app that uses a GUI table,
the data for the table being provided by my Map type.
I'm stuck with the design of the table and the problem is that it uses
row indexing.
If for instance I have 3 Map elements, with keys C, A, B, these are
obviosly stored in the Map in alphabetical order, and then displayed
in my table as rows: 0:A, 1:B, 2C.
But when the user clicks on the table, the only info I recieve is the
row number. I now need to access my Map by index, rather than key.

After fiddling for a while, I've got this to work but it seems very
long winded:

LSysRule::iterator iter;
long i=0;
for(iter = rulesMap->begin(); iter != rulesMap->end(); iter++)
{
i++;
if(i == rowNumber)
FoundIt = *iter:
}

Is there a neater/faster way to do this?

Thanks

Steve

Apr 4 '07 #1
2 8650
Steve555 wrote:
I have a class which makes much use of a Map:

typedef map<char, string, less<char LSysRule;

As you can see, text strings are stored with characters as the key.

I'm trying to re-use the class in another app that uses a GUI table,
the data for the table being provided by my Map type.
I'm stuck with the design of the table and the problem is that it uses
row indexing.
If for instance I have 3 Map elements, with keys C, A, B, these are
obviosly stored in the Map in alphabetical order, and then displayed
in my table as rows: 0:A, 1:B, 2C.
But when the user clicks on the table, the only info I recieve is the
row number. I now need to access my Map by index, rather than key.

After fiddling for a while, I've got this to work but it seems very
long winded:

LSysRule::iterator iter;
long i=0;
for(iter = rulesMap->begin(); iter != rulesMap->end(); iter++)
{
i++;
if(i == rowNumber)
FoundIt = *iter:
}

Is there a neater/faster way to do this?
Not sure what you're going to do if the number is invalid

LSysRule::iterator it = rulesMap->begin();
if (rowNumber < rulesMap->size())
FoundIt = (std::advance(it, rowNumber), *it);
else
throw "Invalid row number";

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Apr 4 '07 #2
On 4 Apr, 16:40, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Steve555 wrote:
I have a class which makes much use of a Map:
typedef map<char, string, less<char LSysRule;
As you can see, text strings are stored with characters as the key.
I'm trying to re-use the class in another app that uses a GUI table,
the data for the table being provided by my Map type.
I'm stuck with the design of the table and the problem is that it uses
row indexing.
If for instance I have 3 Map elements, with keys C, A, B, these are
obviosly stored in the Map in alphabetical order, and then displayed
in my table as rows: 0:A, 1:B, 2C.
But when the user clicks on the table, the only info I recieve is the
row number. I now need to access my Map by index, rather than key.
After fiddling for a while, I've got this to work but it seems very
long winded:
LSysRule::iterator iter;
long i=0;
for(iter = rulesMap->begin(); iter != rulesMap->end(); iter++)
{
i++;
if(i == rowNumber)
FoundIt = *iter:
}
Is there a neater/faster way to do this?

Not sure what you're going to do if the number is invalid

LSysRule::iterator it = rulesMap->begin();
if (rowNumber < rulesMap->size())
FoundIt = (std::advance(it, rowNumber), *it);
else
throw "Invalid row number";

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Thanks Victor, I hadn't come across std::advance yet. Works perfectly
for my needs.
As my table is dynamically resized by whatever data-source is feeding
it, it should be impossible for it to include a row that doesn't
exist. But I'll check for it anyway!

Apr 4 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Gernot Frisch | last post by:
Hi, how can I access the nth element in a std::list?? Or should I use a deque/queue instead? Thank you, -- -Gernot int main(int argc, char** argv) {printf ("%silto%c%cf%cgl%ssic%ccom%c",...
9
by: WalterR | last post by:
This is my first time here, so there may be earlier relevant threads of which I am unaware. Though my experience with DB2 is not extensive, such as it is was under OS/390 or equ. My main...
3
by: David M | last post by:
When using a dataset, how can one get to a column data element using this format: dataset.Tables.Rows.... I can get to a specific row, but I am not sure how to get to the column or data. I...
0
by: Samuel R. Neff | last post by:
I'm having a index problem with ListView SubItems. If I add multiple columns to the listview and then add items with associated subitems, the ListView displays fine. Then if I delete a column via...
1
by: Jason MacKenzie | last post by:
We are running Exhange 2003. Basically, I need to access a specific mailbox for items with a specific phrase in the subject and open a text attachment and work with it. Is this even possible? If...
0
by: dude | last post by:
I was looking on the Internet for info on the index.dat files and found products that delete them using Microsoft APIs which they say is the preferred method. So does anyone know the APIs for...
5
by: Venkatesh Subbaramu | last post by:
Hi Expert DBAs, I am facing an issue with a query, can anyone help me through? I have a Query: UPDATE VMUS.MSG_MSTR a SET MSG_STA_ID =5 WHERE a.PROC_ID =2 AND a.file_type_id =1 AND MSG_STA_ID...
2
by: inpuarg | last post by:
Is it possible that - or is there any workarround for adding a new unbound row to a datagridview at bound mode ? Theese are not working. And i don 't want to add a row to dataset then rebind -...
1
by: amit | last post by:
I'm learning how to upload in PHP and for security reason I'm checking file mime when it is uploaded. Now how can I get access to the mime part. I can get the details using a foreach loop but don't...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.