CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8
  1. #1
    Join Date
    May 2015
    Posts
    500

    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

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  3. #3
    Join Date
    May 2015
    Posts
    500

    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

  4. #4
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: stl map with two keys

    Quote Originally Posted by pdk5 View Post
    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.

  5. #5
    Join Date
    May 2015
    Posts
    500

    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

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    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)

  7. #7
    Join Date
    May 2015
    Posts
    500

    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)

  8. #8
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: stl map with two keys

    Quote 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.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

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
  •  





Click Here to Expand Forum to Full Width

Featured