Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
So we know that the basic structures which form the backbone of C++ algorithms are:
trees set
queue
linkedlist
array
vector map
unordered_map and pair.
My question is which data structure is suitable for which application.For instance I know that for Database indexing and searching preferred choices are B+ tree and Hash table.Can anyone shed some more light on this,
This is not only a C++ problem, but also an algorithm question. It maybe too broad, but I can give you some advice.
set and map: They are ordered container, it is used for a both manytimes-insert-and-read structure. It can finish insert delete read in O(logn) time.
vector: used for something like dynamic array or a structure you will frequently push_back at it, and if no other reason, you should use it.
deque: much like vector, but it can also finish push_front in O(1) time
list: used for a structure you need to frequently insert, but less random access
unordered_map and unordered_set: look for hash table
array: used for a structure whose size is fixed.
pair and tuple: bind many object into one struct. Nothing special
Beside all of this, there are also some container meeting other requirement, you can serach them.
e.g. any and optional
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 2 years ago.
Improve this question
I was solving a question in codeforces,which was about insertion sort,
the link:the problem
I am unable to solve the question the editorial says
old - 2 * (di, ai + dj, aj - di, aj - dj, ai) - 1
I am not understanding the solution kindly help!
here is the editorial link:
https://codeforces.com/blog/entry/9584
For an insertion sort into a linked list, the number of "swaps" is maximum of 2, if you consider inserting a new item as "swapping pointers". The minimal number would be zero, if the key already exists; otherwise you'll swap one pointer when inserting before the head or after the tail.
The concept of an insertion sort is to keep the container sorted. Most of the time is spent searching for the proper place in the container to insert the new datum.
For an array container, you would need to move elements in order to make room for the new data; which doesn't involve swapping.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 4 years ago.
Improve this question
I have a question about lookup speed. I want to know which STL container can produce the fastest lookup time in C++. unordered_map comes to my mind since it is implemented by hash map, but I am afraid its performance is penalized because it contains key-value pair, whereas set contains only key. I guess the answer will depends on 1) the data type of key; and 2) the STL implementation of set.
In other words, which container is faster to search for the existence of an key, is it set, unordered_map, or something else?
Edit:
Would appreciate the answer with more explanation on the implementation or mechanism of the container. For instance, unordered_map is fast because it's implemented with hashmap. That will be more helpful than saying "it depends on the need". Thanks!
This depends to a large degree on the distribution of your data, the size of your dataset, the compiler, the toolchain...
The only way you can know is to measure it for your use case.
Do this after selecting the appropriate container for your task, then switch to something else only if you find that you need to and that you can get better performance for your use case by doing so.
Based on your question, I'd say the choice is between set and unsorted_set. On the other hand, if you don't actually know yet whether your data have both keys and values, then you're probably not ready to start profiling your solution.
Consider this perspective from a different angle,
Since performance is what you're interested in here, you might want to design your data structure in a way to optimally utilise cacheline
If the number of elements is not too high, then a vector will outperform all other containers. This is the case, because vectors store elements in contiguous memory locations and your cache loves contiguous memory allocation
You also mentioned about key-value pairs hampering lookup speed. One way to get around this from cacheline perspective is to store the keys in a contiguous data structure, do the lookup with keys alone. The only when you have a hit, you might want to read the corresponding value from your key-value pair
Check out this talk by Mike acton for more on this
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Somebody told me yesterday that the underlying structure of an ordered map is a binary search tree. This does not make sense to me since you cannot have O(1) retrieval if that were the case. Can anyone explain?
Also, if one were to implement a hash table in C++ without using the stdlib, what would be the best way to do so?
std::map lookup time is not O(1) its O(log(n)).
std::unordered_map has a lookup time of O(1) amortized.
std::unordered_map and std::unordered_set are hashtables.
The underlying data structure is implementation-defined. It is most commonly implemented as a Red-Black tree which is a self-balancing binary search tree. The time complexity for getting an element is O(logn) (see this)
I would just read the implementation of std::unordered_map as a starting point. I assume this is learning activity so reading and understanding working STL implementation would be a good exercise. If it's not an exercise then use std::unordered_map
std::map uses Red-Black tree as it gets a reasonable trade-off between the complexity of node insertion/deletion and searching.
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 6 years ago.
Improve this question
One of my friend has been asked with a question in an interview:
The best possible way to search for a given value among N unsorted numbers in a array.
If the array is unsorted, you need to perform a linear scan of the list. This examines (worst case) every element in the array. Such a search is O(n).
Sorting won't help here, since the best sorts run in O(n log n).
If it was sorted I'd say std::binary_search, but for unsorted, just go with std::find (unless the container you use has a member find; if it does, then use that as it is probably faster).
It depends:
-if you just want to search a single value, the answer given by bush is enough.
-if you know what you want to perform repeated "query", it could be better to perform before some kind of preprocessing,in order to find fastly these value.If you want to know only if a value is contained in the array, you can use structures like hashset,bloom filter,etc..
In other cases,you would like to know also position of items inside the array. In this scenario,you can consider to use an hashmap
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm making a Monopoly game for a school project, written in C++. The first thing I'm working on is implementing the board. It's intuitive to me that each tile will be an object holding information and functions and whatnot, but I cannot decide whether these should be contained in a linked list or array.
It made sense for a linked list because i could simply have the last tile point to the first, but it also seems more efficient to use an array since I can instantly access Tile[5] for example.
Can anybody clarify as to which would be better for this purpose?
It's a fixed size. That negates about 90% of the advantages of a linked list.
You will NEVER be accessing it sequentially (unless, instead of dice, everybody just moves one square each time), but always randomly. That's about 90% of the advantage of an array.
The one reason you cite for using a linked-list is trivially handled differently. (new_position = (current_position + roll) % 40;)
Therefore: You unquestionably want to use an array.