473,387 Members | 1,834 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,387 software developers and data experts.

std::map key usage

5
Hi,
Can we use a structure as a key of a map? If yes how will I search for a particular key.
here goes the example:
Expand|Select|Wrap|Line Numbers
  1. typedef struct radioCardInfo
  2. {
  3.  uint32 radioCardId;
  4.  uint8 radioPathId;
  5. } radioCardInfo;
  6.  
  7. typedef struct msgInfo
  8. {
  9.  uint32 magId;
  10.  enum msg;
  11. } msgInfo;
  12.  
  13. typedef map< radioCardInfo, msgInfo> radioCardMapType;
  14. radioCardMapType  radioCardMap;
  15.  
How will I search radioCardInfo.radioPathId(this should be the key) using map.?

regards
Srini
Mar 3 '07 #1
5 4476
AdrianH
1,251 Expert 1GB
Hi,
Can we use a structure as a key of a map? If yes how will I search for a particular key.
here goes the example:
Expand|Select|Wrap|Line Numbers
  1. typedef struct radioCardInfo
  2. {
  3.  uint32 radioCardId;
  4.  uint8 radioPathId;
  5. } radioCardInfo;
  6.  
  7. typedef struct msgInfo
  8. {
  9.  uint32 magId;
  10.  enum msg;
  11. } msgInfo;
  12.  
  13. typedef map< radioCardInfo, msgInfo> radioCardMapType;
  14. radioCardMapType  radioCardMap;
  15.  
How will I search radioCardInfo.radioPathId(this should be the key) using map.?

regards
Srini
Use an iterator. You can find a good example of it here.

Hope this helps.


Adrian
Mar 4 '07 #2
shrini
5
Use an iterator. You can find a good example of it here.

Hope this helps.


Adrian

Hi... I hope iterator is used incase the key is of a basic datatype.
what about when the key is of the user defined data type like a structure..??
ie, when i need to find (search ) any one of the element of the structure.
here goes the example code that im loking for to search
:typedef mp<struct radioInfo, msgId> radioInfoMaptype;
radioInfoMaptype radioInfoMap

now if I need to search the element of radioInfo (ie radioInfo.radioCardId), how will I do it using the iterator operator ?
radioInfoMap.find(xxxx) xxxx is the element of the structure which is defined as the key of the map

Warm regards
Shrini
Mar 4 '07 #3
AdrianH
1,251 Expert 1GB
Hi... I hope iterator is used incase the key is of a basic datatype.
what about when the key is of the user defined data type like a structure..??
ie, when i need to find (search ) any one of the element of the structure.
here goes the example code that im loking for to search
:typedef mp<struct radioInfo, msgId> radioInfoMaptype;
radioInfoMaptype radioInfoMap

now if I need to search the element of radioInfo (ie radioInfo.radioCardId), how will I do it using the iterator operator ?
radioInfoMap.find(xxxx) xxxx is the element of the structure which is defined as the key of the map

Warm regards
Shrini
Iterator is not an operator, but a class that acts like a pointer. So when invoking find(xxxx), the class that xxxx object is part of must have the operators < and == overloaded, this is so that the pair can be inserted into the map and so that find() can find the object in question. Not overloading these may still work, but it may be slower than what you really wanted.

If you dereference a map's iterator, you get a pair<key, item> object which in your case would be pair<radioInfo, msgId> (you don't need the struct prefix in C++). To access the second item you would do something like this:

Expand|Select|Wrap|Line Numbers
  1. radioCardMapType::iterator myIterator = radioInfoMap(<your structure or reference to structure here>);
  2. msgInfo& myMsgInfo = myIterator->second;
  3.  
Feel free to ask more questions if you still don't understand.


Adrian
Mar 4 '07 #4
shrini
5
Iterator is not an operator, but a class that acts like a pointer. So when invoking find(xxxx), the class that xxxx object is part of must have the operators < and == overloaded, this is so that the pair can be inserted into the map and so that find() can find the object in question. Not overloading these may still work, but it may be slower than what you really wanted.

If you dereference a map's iterator, you get a pair<key, item> object which in your case would be pair<radioInfo, msgId> (you don't need the struct prefix in C++). To access the second item you would do something like this:

Expand|Select|Wrap|Line Numbers
  1. radioCardMapType::iterator myIterator = radioInfoMap(<your structure or reference to structure here>);
  2. msgInfo& myMsgInfo = myIterator->second;
  3.  
Feel free to ask more questions if you still don't understand.


Adrian

Hi Adrian,

I think I still didnt get the solution you said.
My requirement is to have a structure as a key and then, using an iterator I will have to find for the key elements.
say if my structure(key) has 3 elements and each iterator::find(xxxx) I need to search for different elements of the structure.

In first iterator::find(xxxx) I need to find for the key radioCardInfo.radioCardId.
in my second invokation immediately iterator::find(xxxx), I need to find for radioCardInfo.radioCardValue.
How can the iterator class find for a different key this time.
thats why I thought of using a structure as a key element of a map and ecah time I will search for different elements of that structure.

Is that the iterator::find(xxxx) can find for different elements of the structure(key) ??

regards
Srini
Mar 4 '07 #5
AdrianH
1,251 Expert 1GB
Hi Srini,

Ok, I think I understand what you are asking, the key has two (or more) elements in it, and you want to search for a (key, value) pair based on one of the sub-keys, and then search for a (key, value) pair based on the other sub-key. If this is the case then unfortunately, what you are asking for cannot be done with a single map efficiently, though it can be done, but if done it cannot be done exclusively with the map::find() function. The reason is this:

A map is based on a key that has an “order”, this means that a key A and a key B can be compared and one can be said to precede the other. When inserting into the map, the map structure sorts by the (key, value) pair. If that pair already exists in the map, then the old value is overwritten, otherwise it is inserted into the map.

Now, if you were to use map::find() to find a (key, value) pair based on the key, the map will do a lookup that will basically split all of the inserted pairs in half to determine which half the key that is being looked for is in. It will then split that half into half again to determine which quarter the pair would be in. This keeps going till it either finds the key or it doesn’t.

Consider this: if you were to insert and find a key based on sub-key A, and then using the same map, try to find a key based on sub-key B, what would happen if the order of all the values in sub-key A were not the same order as the values in sub-key B. Then finding a key based on sub-key B could conceivably not find the matching key even if one exists since it would be looking in the wrong place for it.

The iterator is not an operation, but a structure that keeps track of where it is in the set of pairs. It acts like a pointer. Say you have an array of 2 elements, and you have a variable that points to the first element. Now you increment that variable and it will now point at the next element. Decrement it and it points back at the first element again. Increment it twice and it will fall off the end (will point to a position that is past the end of the array). This is exactly how an iterator works. If you point at the first element in the map, vector, set, or any other container that supports an iterator that has 2 elements and you do the same thing as I had explained with the array example, you will get the same logical result.

The function map::find() however will find an item in O(log n) time (means that in the worst case if there are n elements it will not have to search every element to find the appropriate element or find that the element does not exist in the map).

If you wanted to find a key based on two separate sub keys, then you would either have two different maps, each one being ordered on different sub-keys or you can have one map that orders on one of the sub-keys and if you want to find on a second sub-key, you would have to find it in O(n) time (means in the worst case you will have to search each and every element to determine if it is there or not). If time is not an issue or n is a small number or space is an issue (or if it is an assignment that your teacher/prof has told you to do), using one map may be what you are looking for.

Iterators are fairly easy to use because they are based on how pointers are used. To get to the beginning of a container, use the <container>.begin() position, you can then search each pair returned by dereferencing the iterator for the second criteria that you are looking for till you reach the <container>.end() position. Do not search that position as it is off the end of the container (just like in the array example).

There are already algorithms in the STL that will do this second search, namely find_if(), but it requires another class to be made, and I want to make sure that you have understood everything that I have said so far, and if going further is necessary for you to do your assignment.

Let me know.



Adrian
Mar 5 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Antti Granqvist | last post by:
Hello! I have following object relations: Competition 1--* Category 1--* Course 1 | | * Course
44
by: jmoy | last post by:
I am a C programmer graduating to C++. As an exercise I wrote a program to count the number of times that different words occur in a text file. Though a hash table might have been a better choice,...
1
by: Saeed Amrollahi | last post by:
Dear All C++ Programmers Hello I am Saeed Amrollahi. I am a software engineer in Tehran Sewerage Company. I try to use std::map and map::find member function. I use Visual Studio .NET. my...
5
by: Peter Jansson | last post by:
Hello, I have the following code: std::map<int,std::set<std::string> > k; k="1234567890"; k="2345678901"; //... std::set<std::string> myMethod(std::map<int,std::set<std::string> > k)...
19
by: Erik Wikström | last post by:
First of all, forgive me if this is the wrong place to ask this question, if it's a stupid question (it's my second week with C++), or if this is answered some place else (I've searched but not...
3
by: Dan Trowbridge | last post by:
Hi everyone, In my attempt to port code from VS 6.0 to VS.NET I had some code break along the way, mostly due to not adhereing closely to the C++ standard. This may be another instance but I...
1
by: Avery Fong | last post by:
The following program will result in a compile error when building under Debug but will compile under Release. Why does is work under Release mode but not under Debug This program is developed...
13
by: kamaraj80 | last post by:
Hi I am using the std:: map as following. typedef struct _SeatRowCols { long nSeatRow; unsigned char ucSeatLetter; }SeatRowCols; typedef struct _NetData
5
by: Yannick Tremblay | last post by:
Hi, I have a std::map<id, timestampcollection. The id is the normal reference so the key-value ordering is correct. However, once in a blue moon, I want to prune this collection. So that...
10
by: desktop | last post by:
If I have: std::map<int,intm; where I have: m.insert(std::make_pair(1,2)); how do I update the value to 7 associated with key = 1?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.