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

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.

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.

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

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)

What is the algorithmic complexity of malloc()/free()/new/delete/delete[]? [duplicate]

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.

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.