|
-
May 22nd, 2015, 09:59 AM
#4
Re: stl map with two keys
 Originally Posted by pdk5
Thanks a lot 2kaud. Looks like the iterator is invalid..
std::map<std:: pair<string, u32>, CPGW::CreateSessInfo>::iterator it =
CPGW::m_mSessionId2CCRNum2CreateSessionInfo.find(std::make_pair(sessionid, ccreqnum));;
if( it != CPGW::m_mSessionId2CCRNum2CreateSessionInfo.end())
{
CreateSessInfo stCrSessInfo(it->second);
// Do some processing
}
else
{
LogError("Invalid Iterator in CPGW::OnPCRFCCA " );
}
It is going into LogError..Not sure if there is any issue with calling the iterator for the map with two keys
"Invalid Iterator" might be a missleading message here. Or at least. Not very helpful. It is only a consequence of the fact that key you are looking for is missing. So "Key not found in CPGW::OnPCRFCCA" would be more useful here. You could also log said key.
Not sure what you mean by "Not sure if there is any issue with calling the iterator for the map with two keys"? You are basically just creating a map with a single key, which is a pair, and a pair has a defined sorting order. So it is a completely fine approach.
An added bonus (if you care about ordering), is that your elements will be sorted according to lexicographical ordering. EG: Sort first by the first key, then the second.
If you don't care about ordering at all, consider using an unordered_map instead. That will, however, require you write your own hash. Which is a bit complicated, so only do it if you feel comfortable with it.
PS: typedef's and auto are your friend.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|