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 5 4026
Josef Meile <jm****@hotmail.comwrote in news:1179501045.183061.145130
@h2g2000hsg.googlegroups.com:
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";
}
Josef Meile wrote:
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.
On 18 May 2007 08:10:45 -0700 in comp.lang.c++, Josef Meile
<jm****@hotmail.comwrote,
>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" ?
On May 18, 5:25 pm, Andre Kostur <nntps...@kostur.netwrote:
Josef Meile <jme...@hotmail.comwrote in news:1179501045.183061.145130
@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.)
--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
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
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;
Is there any way of getting the key as well?
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Diego Barros |
last post by:
Hello,
I was wondering if it was posibble to sort a map using the values and not
the keys. Sorting on the keys (if they were strings, for example) is
currently possible. What about, for example,...
|
by: Juicer_X |
last post by:
Hello,
I have some code that I'm working on, the problem isn't that it doesn't
work it's that it's too slow. I have a class that holds my homemade class
within an std::map, within an std::map....
|
by: andreas |
last post by:
Hello,
I have a problem with iterators in a fairly complex polygonal mesh data
structure which is implemented using lists of geometric entities.
However, the problem in itself is fairly simple:...
|
by: Joseph Garvin |
last post by:
When I first came to Python I did a lot of C style loops like this:
for i in range(len(myarray)):
print myarray
Obviously the more pythonic way is:
for i in my array:
print i
|
by: homsan toft |
last post by:
Hi,
I'm (still) trying to return a pair<const Key, T> from
iterator dereference. So I defined a proxy class in the
obvious way:
template<class KeyT, class DataT>
struct ref_proxy
{
typedef...
|
by: Marcus |
last post by:
I have a vector within a vector within a map. The innermost vector
houses a struct with some ints and floats. The map keys off the
secondary vector which acts as a wrapper for multiple vector...
|
by: John Salerno |
last post by:
I'm a little confused. Why doesn't s evaluate to True in the first part,
but it does in the second? Is the first statement something different?
False
print 'hi'
hi
Thanks.
|
by: yaru22 |
last post by:
When I look at the C++ reference, erase method for map takes an
iterator as a parameter. I'm wondering why it takes iterator instead
of key value. Wouldn't it be faster to take key value and erase...
|
by: Generic Usenet Account |
last post by:
Hi,
I am passing a container type (e.g. list<int>) to a function template
but I am getting a compiler error when I try to declare an iterator
for that type in the function. However, I am able...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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,...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
| |