-
June 2nd, 2016, 07:00 AM
#1
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
-
June 2nd, 2016, 09:32 AM
#2
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)
-
June 2nd, 2016, 10:22 AM
#3
Re: How to avoid an empty entry when an empty slot is found?
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.
Originally Posted by 2kaud
how come unitsToFindPath is defined twice as different types?
I'm guessing that the first declaration should declare window instead.
-
June 2nd, 2016, 12:12 PM
#4
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)
-
June 2nd, 2016, 03:13 PM
#5
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.
-
June 2nd, 2016, 10:58 PM
#6
Re: How to avoid an empty entry when an empty slot is found?
Thanks, I'll make use of the find function.
-
June 3rd, 2016, 06:03 AM
#7
Re: How to avoid an empty entry when an empty slot is found?
Originally Posted by lucky6969b
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|