This question already has answers here:
std::queue iteration
(10 answers)
Closed 3 years ago.
I was trying to iterate over a STL stack in c++ but was unable to do so.
Is it even possible to iterate over a C++ STL Stack or Queue without popping(Like vectors)?
No, you cannot iterate over a std::queue since that is not its purpose.
A container that allows fast insertion at both ends, as well as iteration, is std::deque. Note that iteration is slower than for a std::vector, but insertion/removal at the beginning is much faster.
Related
This question already has answers here:
Vector clear vs. resize
(4 answers)
Closed 4 years ago.
for a vector
std::vector<int> vec;
what's the difference between vec.clear() and vec.resize(0) ?
And if I want to clean a vector, what might be the best practice? (mainly for performance and efficiency concern)
A C++ standard library is allowed to implement vec.clear() as vec.resize(0) so they may well not be distinguishable. Note that neither function is allowed to reduce the capacity.
Personally I'd use clear() as that ever-so-slightly better signals your intent.
This question already has answers here:
How to choose between map and unordered_map?
(5 answers)
Closed 5 years ago.
According to: This Tutorial
I can't understand the difference between std::map and std::unorderedmap. When and Why we should use Map and Unorderedmap?
As I've read in tutorial you provided, search speed in std::unorderedmap is O(1). While in std::map it's O(log2(n)), where n is size of the map.
So if you have to call std::find often, you can consider this option. While choosing hash function isn't an easy task.
This question already has answers here:
What really is a deque in STL?
(8 answers)
Closed 6 years ago.
I am learning stl and learn how to use all stl containers and i need to knew
when i have to use deque in my program.
what is the different between deque and other stl containers
http://www.cplusplus.com/reference/deque/deque/
You can push elements to a deque on both ends.
Example:
You can use a deque as "history".
The oldest items are in front.
Now you have easy access on the newest element and the oldest. (It is good for a undo function)
This question already has answers here:
Time complexity of memory allocation
(5 answers)
Closed 7 years ago.
I would suspect that delete[] is O(n) and the others are O(1), but I want to be sure. Then again, it doesn't seem right that you would get constant time for allocating a variable amount of memory.
None. None of those functions are algorithms. You're not guaranteed any particular complexity from them. They have the complexity of whatever underlying algorithm is used by the implementation.
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Thread safety of std::map for read-only operations
Having std::map a can we do a.find(...)->second in multiple threads at the same time on it?
Yes. As long as none of your threads do a write
i.e. Construct the data structure in memory
Use as many threads to find/read as you require.
If the leaf needs altering put a mutex there.