CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    How to avoid an empty entry when an empty slot is found?

    Code:
    std::map<int, std::vector<CPF_SimObject*>> unitsToFindPath;
    
    std::vector<CPF_SimObject*> unitsToFindPath = window[currentWindow];
    when the currentWindow is not found, unitsToFindPath is added with a new entry,
    how can I avoid that from happening?
    Thanks
    Jack

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

    Re: How to avoid an empty entry when an empty slot is found?

    Is window a map? With a map, operator[] will add a new entry if the specified one doesn't exist. See http://www.cplusplus.com/reference/map/map/operator[]/ If you use .at() and it doesn't exist then an exception will be thrown. Alternatively use .count() to determine if it exists or not. If it doesn't then it returns 0. See http://www.cplusplus.com/reference/map/map/count/

    PS how come unitsToFindPath is defined twice as different types?
    Last edited by 2kaud; June 2nd, 2016 at 09:34 AM.
    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
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: How to avoid an empty entry when an empty slot is found?

    Quote Originally Posted by 2kaud
    With a map, operator[] will add a new entry if the specified one doesn't exist. See http://www.cplusplus.com/reference/map/map/operator[]/ If you use .at() and it doesn't exist then an exception will be thrown. Alternatively use .count() to determine if it exists or not. If it doesn't then it returns 0.
    Although it is quite clear that the use of operator[] here is a bug since adding a new entry when currentWindow is not found is undesirable, I don't think switching to use at() is necessarily the best fix. Rather, consider: is currentWindow not being found an expected scenario that happens relatively often? If so, then perhaps it makes more sense to call find() and check for an end iterator, with the slight inconvenience of having to dereference the iterator if found. But if currentWindow is normally expected to be found, then perhaps using at() would be both more convenient and express this.

    While calling count() will work, it seems a little inefficient since you presumably want to use the element found instead of searching for it again should count find that it exists.

    Quote Originally Posted by 2kaud
    how come unitsToFindPath is defined twice as different types?
    I'm guessing that the first declaration should declare window instead.
    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

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

    Re: How to avoid an empty entry when an empty slot is found?

    While calling count() will work, it seems a little inefficient since you presumably want to use the element found instead of searching for it again should count find that it exists.
    In that case then .find() can be used. See http://www.cplusplus.com/reference/map/map/find/
    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)

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

    Re: How to avoid an empty entry when an empty slot is found?

    +1 for find

    Code:
    auto it = window.find(currentWindow);
    if (it != window.end()) {
      ...
    }
    Alternatively, depending on the goal, you can use lower_bound if you plan to follow up with some sort of insert so you can hint (provided you have a tight need for performance).

    You can also try reading Effective STL by Scott Meyers. Item 24: "Choose carefully between map:perator[] and map-insert when efficiency is important." While this is not your goal, it does have a lot of context and explanation with regards to maps.
    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.

  6. #6
    Join Date
    Dec 2010
    Posts
    907

    Re: How to avoid an empty entry when an empty slot is found?

    Thanks, I'll make use of the find function.

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

    Re: How to avoid an empty entry when an empty slot is found?

    Quote Originally Posted by lucky6969b View Post
    Thanks, I'll make use of the find function.
    Not to rant or anything, but you are close to 900 threads here over a span of 6 years. Surely you should know how to use a map by now? It's a super basic question, and one you've already asked too:
    http://forums.codeguru.com/showthrea...rtain-position

    I think it's time you put effort into actually learning the language. We've certainly spent plenty effort helping you.
    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.

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