Accessing key and value from an Iterator

J

Josef Meile

Hi,

I have a Map of records and I know how to iterate over it and get the
records stored on it. ie:

struct featureRecord
{
featureRecord(CAMERA_FEATURE fId = FEATURE_INVALID_FEATURE,
int val = -1) : featureId(fId), value(val)
{};
//CAMERA_FEATURE is an enum defined in a third party library
CAMERA_FEATURE featureId;
int value;
};

map <string, featureRecord> _features;
_features["brightness"] = featureRecord(FEATURE_BRIGHTNESS);
_features["exposure"] = featureRecord(FEATURE_AUTO_EXPOSURE);
_features["focus"] = featureRecord(FEATURE_FOCUS);

map <string, featureRecord>::iterator iter = _features.begin();
while (iter != _features.end())
{
featureRecord rec = iter->second;
printf("feature id: %d",rec.featureId);
++iter;
}

Is there any way of getting the key as well? ie: printing something
like:

Feature brightness, id: 0
Feature exposure, id: 1
Feature focus, id: 2

I found that qt has an special Map iterator that does this:
* Qt 4's New Style of Iterators -> The Qt 4 Alternative: Java-Style
Iterators
http://doc.trolltech.com/qq/qq12-qt4-iterators.html

Where you have an iterator that has a value() and key() method, but I
don't want to
use qt since it seems to be a big library.

Regards
Josef
 
A

Andre Kostur

Hi,

I have a Map of records and I know how to iterate over it and get the
records stored on it. ie:

struct featureRecord
{
featureRecord(CAMERA_FEATURE fId = FEATURE_INVALID_FEATURE,
int val = -1) : featureId(fId), value(val)
{};
//CAMERA_FEATURE is an enum defined in a third party library
CAMERA_FEATURE featureId;
int value;
};

map <string, featureRecord> _features;
_features["brightness"] = featureRecord(FEATURE_BRIGHTNESS);
_features["exposure"] = featureRecord(FEATURE_AUTO_EXPOSURE);
_features["focus"] = featureRecord(FEATURE_FOCUS);

map <string, featureRecord>::iterator iter = _features.begin();
while (iter != _features.end())
{
featureRecord rec = iter->second;
printf("feature id: %d",rec.featureId);
++iter;
}

Is there any way of getting the key as well? ie: printing something
like:

Feature brightness, id: 0
Feature exposure, id: 1
Feature focus, id: 2

Sure. Recall that the value_type of a map is pair<K, V> (where K is the
key's type, and V is the value's type). So just like you did iter->
second to get the second half of the pair (the V), you can do iter->first
to get the first half of the pair (the K).

for (map <string, featureRecord>::iterator iter = _features.begin();
iter != _features.end(); ++iter)
{
cout << "feature " << iter->first << ", id: " << iter->second << "\n";
}
 
F

Fei Liu

Josef said:
Hi,

I have a Map of records and I know how to iterate over it and get the
records stored on it. ie:

struct featureRecord
{
featureRecord(CAMERA_FEATURE fId = FEATURE_INVALID_FEATURE,
int val = -1) : featureId(fId), value(val)
{};
//CAMERA_FEATURE is an enum defined in a third party library
CAMERA_FEATURE featureId;
int value;
};

map <string, featureRecord> _features;
_features["brightness"] = featureRecord(FEATURE_BRIGHTNESS);
_features["exposure"] = featureRecord(FEATURE_AUTO_EXPOSURE);
_features["focus"] = featureRecord(FEATURE_FOCUS);

map <string, featureRecord>::iterator iter = _features.begin();
while (iter != _features.end())
{
featureRecord rec = iter->second;
printf("feature id: %d",rec.featureId);
++iter;
}

Is there any way of getting the key as well? ie: printing something
like:

Feature brightness, id: 0
Feature exposure, id: 1
Feature focus, id: 2

I found that qt has an special Map iterator that does this:
* Qt 4's New Style of Iterators -> The Qt 4 Alternative: Java-Style
Iterators
http://doc.trolltech.com/qq/qq12-qt4-iterators.html

Where you have an iterator that has a value() and key() method, but I
don't want to
use qt since it seems to be a big library.

Regards
Josef

For STL map, iter->first points to the key and iter->second points to
the value.
 
D

David Harmon

On 18 May 2007 08:10:45 -0700 in comp.lang.c++, Josef Meile
while (iter != _features.end())
{
featureRecord rec = iter->second;

That makes a copy of the featureRecord that you probably don't need.
To avoid copying, consider

featureRecord const & rec = iter->second;
Is there any way of getting the key as well?

Did "iter->second" not make you wonder about "iter->first" ?
 
J

James Kanze

@h2g2000hsg.googlegroups.com:
Sure. Recall that the value_type of a map is pair<K, V>
(where K is the key's type, and V is the value's type).

Just a nit, but the value_type is pair< K const, V >. This
means that something like:

std::map< std::string, int >::iterator
it ;
std::map< std::string, int >::value_type
v ;
v = *it ;

won't compile. (Normally, of course, you'll use it->first and
it->second to access the individual parts directly.)
 
J

Josef Meile

while (iter != _features.end())
That makes a copy of the featureRecord that you probably don't need.
To avoid copying, consider
Thanks, that's a nice tip. I left C++ long time ago, so, I can say I'm
beginning again.
featureRecord const & rec = iter->second;


Did "iter->second" not make you wonder about "iter->first" ?
Yes, it did. Somewhere I found that if you had a map of strings "iter-
first" would be the
first character of the string's value. Perhaps the reference I found
was wrong. I will try
this anyway.

Thanks all for the help

Regards
Josef
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top