-2

So similarly to my previous question, I wanted to store a few attributes as a key for my map and the value will also be a different struct.

I now have a map like so:

std::map<SKey, SValue> m_mMap;

SKey has the following structure:

Struct SKey
{
    string str1,
    string str2,
    unsigned long long ull1
};

I want to implement the quickest way to retrieve an iterator of a match to all these variables. So something like this:

std::map<SKey, SValue>::iterator iter = m_mMap.find(m_sKey);

What I have so far to check if there is a match in the map and to retrieve the iterator is the following:

inline bool operator<(const SKey& lhs, const SKey& rhs)
{
    return std::tie(lhs.strSrcAddr, lhs.strDstAddr, lhs.ullSequenceNo) < std::tie(rhs.strSrcAddr, rhs.strDstAddr, rhs.ullSeqNo);
}

Is this the correct way to find a match of an SKey object? Or is there a quicker way? I've tested this but i'm not entirely convinced it works because it seems to return false when the left side and the right side are the same. What am i doing wrong? Any help or advice would be appreciated and if there is a quicker way of doing this i'd appreciate it because my program is very concentrated on performance.

Thanks

  • 3
    This isn't real code. Don't invent pseudo-code. Copy *compilable* code from your editor. – Kerrek SB Aug 25 '14 at 23:00
  • Sorry I was worrying about sharing company code... I guess I was worrying too much – Patricia Aydin Aug 25 '14 at 23:48
  • That has nothing to do with anything. You can open a new, blank file away from your company and create a minimal example. Just don't post it until it compiles, and post it by copy-pasting the code. – Kerrek SB Aug 25 '14 at 23:56
  • Ok. Next time I'll post all of the above code but with the correct variable names. "That has nothing to do with anything" seems to be a real polite way of saying "that isn't the issue". Thank you VERY much. – Patricia Aydin Aug 26 '14 at 12:53
  • Variable names aren't so important. Spelling `struct` right, however, *is* important, which is why you should absolutely make sure that your code compiles. – Kerrek SB Aug 26 '14 at 13:02
  • Could you point me to the place where I spelt "struct" incorrectly? And i'll keep that in mind in the future. Copy and paste job it is. – Patricia Aydin Aug 26 '14 at 13:28

1 Answers1

0

I've tested this but i'm not entirely convinced it works because it seems to return false when the left side and the right side are the same. What am i doing wrong?

inline bool operator<(const SKey& lhs, const SKey& rhs)
{
    return std::tie(lhs.strSrcAddr, lhs.strDstAddr, lhs.ullSequenceNo) < std::tie(rhs.strSrcAddr, rhs.strDstAddr, rhs.ullSeqNo);
}

This line SHOULD return false when the left and right sides are the same due to the property of the less than operator. If you want it to return true, you should use the <= operator.

MARS
  • 101
  • 9
  • Don't make assumptions when answering unclear questions! – πάντα ῥεῖ Aug 25 '14 at 23:11
  • All I want is for the find function to return the iterator containing an object with a match or without a match. I was asking if the code I wrote returns an iterator containing a matched object based on the variables on the left being identical to the variables on the right and no other instance would deem the two to be the same I.e. a match. Also is this the quickest way to check if an element is in a map (but the map being a struct struct)... – Patricia Aydin Aug 25 '14 at 23:51
  • @PatriciaAydin You should make this clear in your original post. If all you are looking for is an exact match within your map, I would recommend to not use a map at all and just add a value member to your struct. Then put all of your structs inside a vector. Why do you need to have a map? – MARS Aug 26 '14 at 00:04
  • Hmm... i guess using a vector will be equally simple. Thanks :). Does it make a difference performance wise? – Patricia Aydin Aug 26 '14 at 12:52
  • @PatriciaAydin You may want to refer to this question if you want to know about map performance: http://stackoverflow.com/questions/10165708/stl-map-performance. To summarize, a map finds objects in O(logn) time. A vector is basically an array, so the time complexity is O(n). However, it is still unclear what exactly you want. If all you want to do is prove whether or not something exists in your collection of structs, just use the vector because it would take O(n) time to prove that something doesn't exist anyway. You also wouldn't have to define a < operator. – MARS Aug 26 '14 at 17:20