what is the explanation of deque can any one help me? [duplicate] - c++

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)

Related

std::vector clear or swap with new vector [duplicate]

This question already has answers here:
Does clearing a vector affect its capacity?
(4 answers)
C++ delete vector, objects, free memory
(7 answers)
Closed 3 years ago.
Sometimes I detect in different c++ projects code like this:
std::vector<DataClass> _dataVec;
...
std::vector<DataClass>().swap(_dataVec);
Is this code more effective than obvious and simple clear call
_dataVec.clear();
or these code samples have some kind of difference?
For what purpose I should prefer first variant?

Iterate over C++ STL queue [duplicate]

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.

C++ vector: difference between clear() and resize() [duplicate]

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.

What is difference between map and unordered map? [duplicate]

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.

Should we prefer vector<unique_ptr<>> or boost::ptr_vector [duplicate]

This question already has answers here:
stl container with std::unique_ptr's vs boost::ptr_container
(2 answers)
Closed 8 years ago.
Now with C++11, should we prefer vector<unique_ptr<>> or boost::ptr_vector to store pointer's to objects if we want managed memory?
I would suggest vector<unique_ptr<>> as it is supported by compiler. no extra effect. otherwise, boost need your import into your project.