-
May 22nd, 2015, 08:16 AM
#1
stl map with two keys
Hi,
I have a map with two keys:
std::map<std:: pair<string, u32>, CPGW::CreateSessInfo> CPGW::m_mSessionId2CCRNum2CreateSessionInfo;
Now some where in the code:
{
std::map<std:: pair<string, u32>, CPGW::CreateSessInfo>::iterator it;
it = m_mSessionId2CCRNum2CreateSessionInfo.find(std::make_pair(sessionid, ccreqnum));
}
iterator is giving some wrong garbage.
Could some stl c++ experts help me with your knowlwdge please. Pardon me for my basic question..
Also help in improving the code is appreciated..
thanks,
pdk
-
May 22nd, 2015, 08:28 AM
#2
Re: stl map with two keys
iterator is giving some wrong garbage.
Have you checked that the iterator is not .end() - ie key not found?
Also you have
Code:
std::map<std:: pair<string, u32>, CPGW::CreateSessInfo> CPGW::m_mSessionId2CCRNum2CreateSessionInfo;
...
it = m_mSessionId2CCRNum2CreateSessionInfo.find(std::make_pair(sessionid, ccreqnum));
using namespace CPGW in the first but not in the second?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 22nd, 2015, 09:46 AM
#3
Re: stl map with two keys
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
-
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.
-
May 22nd, 2015, 10:33 AM
#5
Re: stl map with two keys
Thanks a lot monarch_dodra for hints and useful suggestions...
I think it was my mistake, there was some issue in the wrongly hardcoding of the second key somewhere..We donot have a good debug and test setup..so took sometime to identify this.
Being a newbie to c++, stl, your help and comments are really appreciated
-
May 22nd, 2015, 01:47 PM
#6
Re: stl map with two keys
Being a newbie to c++, stl,
You might find these sites of interest
http://www.cplusplus.com/reference/ and clicking through the various header files
http://www.cplusplus.com/doc/tutorial/
http://www.learncpp.com/
There's also a whole range of various(fairly expensive!) books on c++ and the STL. I would suggest
Professional c++ (Third edition)
http://www.amazon.co.uk/Professional...sional+c%2B%2B
The various books by Scott Meyers
The c++ Standard Library Second Edition
http://www.amazon.co.uk/books/dp/032...=stl+reference
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
May 26th, 2015, 08:46 AM
#7
Re: stl map with two keys
Thanks a lot Kaud..
I have ScottMayers STL book, but I find it bit advanced..(may be when i started coding in c++, from a basci c coder)
-
May 26th, 2015, 11:40 PM
#8
Re: stl map with two keys
Originally Posted by pdk5
I have ScottMayers STL book, but I find it bit advanced..(may be when i started coding in c++, from a basci c coder)
Scott Meyers' "STL book" (i.e., "Effective STL") is aimed at C++ programmers who already have some background in the portion of the standard library derived from the Standard Template Library, but wish to learn more or to avoid possible bad practices. 2kaud's mention of "The C++ Standard Library, Second Edition", on the other hand, is written by Nicolai Josuttis, who wrote that book with both beginners and experienced C++ programmers in mind. It has more comprehensive coverage of the standard library rather than covering only those parts derived from the STL.
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
|