STL Map; How to access a specific index?

S

Steve555

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
 
V

Victor Bazarov

Steve555 said:
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
 
S

Steve555

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

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top