This question already has answers here:
Is the order of items in a hash_map/unordered_map stable?
(2 answers)
Closed 7 years ago.
Context: I need this data structure to find the keywords of a particular file at a node. So the map is having file name and vector to store the keywords of that file.This is basically a small code i am using in mpi to find relationship between files at different nodes in a parallel fashion.
The order of the elements of an std::unordered_map is not stable, which explains your output.
Read this answer for more and how to read the elements. Also next time search before asking a question and of course (almost) never post images of code, but use code tags.
From the ref:
Internally, the elements are not sorted in any particular order, but organized into buckets.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I have a list of unsigned shorts that act as local IDs for a database. I was wondering what is the most memory-efficient way to store allowed IDs. For the lifetime of my project, the allowed ID list will be dynamic, so it may have more true or more false allowed IDs as time goes on, with a range of none allowed or all allowed.
What would be the best method to store these? I've considered the following:
List of allowed IDs
Bool vector/array of true/false for allowed IDs
Byte array that can be iterated through, similar to 2
Let me know which of these would be best or if another, better method, exists.
Thanks
EDIT: If possible, can a vector have a value put at say, index 1234, without all 1233 previous values, or would this suit a map or similar type more?
I'm looking at using an Arduino with 2k total ram and using external storage to assist with managing a large block of data, but I'm exploring what my options are
"Best" is opinion-based, unless you are aiming for memory efficiency at the expense of all other considerations. Is that really what you want?
First of all, I hope we're talking <vector> here, not <list> -- because a std::list< short > would be quite wasteful already.
What is the possible value range of those ID's? Do they use the full range of 0..USHRT_MAX, or is there e.g. a high bit you could use to indicate allowed ones?
If that doesn't work, or you are willing to sacrifice a bit of space (no pun intended) for a somewhat cleaner implementation, go for a vector partitioned into allowed ones first, disallowed second. To check whether a given ID is allowed, find it in the vector and compare its position against the cut-off iterator (which you got from the partitioning). That would be the most memory-efficient standard container solution, and quite close to a memory-optimum solution either way. You would need to re-shuffle and update the cut-off iterator whenever the "allowedness" of an entry changes, though.
One suitable data structure to solve your problem is a trie (string tree) that holds your allowed or disallowed IDs.
Your can refer to the ID binary representation as the string. Trie is a compact way to store the IDs (memory wise) and the runtime access to it is bound by the longest ID length (which in your case is constant 16)
I'm not familiar with a standard library c++ implementation, but if efficiency is crucial you can find an implementation or implementat yourself.
This question already has answers here:
Find mapped value of map
(6 answers)
Closed 1 year ago.
std::unordered_map::find search for the certain key in the unordered_map, and is there a function to search a certain value?
Definitely I can write some simple loop to do it but maybe something already exist for that that?
No, not really. The map entries (key-value pairs) are not arranged according to their values; nor are the values stored separately from the keys etc. Or rather - the standard doesn't guarantee any of that, and all popular implementations don't offer this.
You're just going to have to use a linear search... or a different/additional data structure which supports the kind of searches you need.
Also remember that std::unordered_map is quite slow in practical, non-asymptotic terms, so if performance is a consideration - definitely look for alternatives.
This question already has answers here:
Similarity String Comparison in Java
(12 answers)
Closed 4 years ago.
I am trying to get an algorithm that tells you what percentage of similarity there is between two sentences. I was thinking of creating a vector of chars. for each char there is in the sentence compare to all of the other chars in another sentence. then the amount of characters that are the same over the total amount of characters should give me that %... but If you guys have a faster, and more efficient way of doing this. then it would be greatly appreciated.
What you are looking for might be an algorithm such as the Vector Space Model [wiki link]. It is a common algorithm web search engines use to come up with relevant sites to strings, that users put in.
It is not the only algorithm that does this kind of thing (comparing text and giving a value for similarity), but most of them are not overly complicated and there are already libraries in C++, which implement them effectively for example Lucene or Xapian. If you skip through their docs you will almost certainly find a function that just takes two strings and gives back a scalar representation of their semantic similarity.
You could use the Levenshtein distance to work out the similarity between the two strings - see https://en.m.wikipedia.org/wiki/Levenshtein_distance for more info
This question already has answers here:
When to use a linked list over an array/array list?
(15 answers)
vector vs. list in STL
(17 answers)
Closed 8 years ago.
sorry if this been asked already...
Why and when Should I use linked lists over vectors? I just don't like all those pointer overheads...
From my knowledge: vector is faster, more compact because there's no extra pointers, and is easier to implement; also I think that linked lists do not exploit the principle of spatial locality because nodes are in totally random memory locations so your code becomes slower... so when you are using linked lists you are increasing your cache misses which you don't want to do...
Of course the advantage of lists is that you can avoid overflows with dynamic memory allocation...
In summation, my question is: where should you use, if ever, linked lists over vectors? which data struct do you prefer more?
Linked Lists are for situations where you want to insert or remove an item without shifting or insert/push or pop item in constant time and when you don't know the number of elements and maybe don't need a random access. for more information see this .
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm intrested in creating programs of games and such which learns while playing and saving the information for further use. For example a tic tac toe game where the program saves evry game it won or lost and creates a tree of some kind that store the information of the game and save it in a file when the program is being quit. The problem i'm having is how to save a tree in a file efficently. Any suggestions?
Thenx in advance.
(I'm programming in c++)
One option is to use Boost Property Tree, that can help you to load and store tree structures into the xml files (and several other formats are supported as well). This library is well documented, you can find many examples directly on the web-site
What you're looking for is also called serialization. There are quite a few hits here on StackOverflow for serializing trees in C++. I suggest you look into JSON and YAML as possible formats. There are multiple C++ libraries for both.
I think I'd try nogard's answer first (I've upvoted his). However, if you find that you don't get much value out of the runtime support (ie. the tree in memory) and it only really serves as a serialization/unserialization mechanism, I'd suggest trying out pugixml. It's a very complete, very performant, yet simple to integrate, xml library.
Furthermore, to aide in the in-memory part, I've had good success with simply composing trees out of STL containers (it's simpler than it seems). I've found that different trees can have different requirements (ie. pointer to parent, no pointer to parent, etc.) If the tree has a large number of nodes, these pointers per node can add up. And, if you don't have a use for them, given the way you're iterating through your tree, then it'll just be waste.
If your tree is a stl container, such as a map, then you can create a functor for each element that serialises its data to a string, or directly to file, then walk the tree - using an algorithm like for_each which will call the functor for you.
That's simple. Re-creating the tree is a matter of reading the elements from the file, turning them back into objects and adding them to a new tree structure. You'll need to keep the keys to the tree, not just the values.