I want to hold a group of iterators from a seach function. Is there a ‘better way’ to hold a group of iterators (arrays vs. sets). What are the pros and cons of each in terms of memory efficiency?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It doesn’t make sense to talk about which is better array or set without understanding what you are trying to accomplish.
There’s a consideration you should think about: what kind of container that the iterators are pointing at, and is that container is expected to be being updated?
For example, if you are storing iterators to a vector (doesn’t matter where you put these iterators), and you update the vector, the previously store iterators are possibly invalid. Be very careful of storing iterators.
It sounds like you are caching results for speed. If you need to cache results, you might be better off using one of the unordered containers. Use the same key for your search results as you do for your cache. Don’t store the iterators, just the query key and the actual result. The unordered_set has O(1) lookup time and they don’t take up very much space as they hash the key and store the results.