std::vector clear or swap with new vector [duplicate] - c++

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?

Related

What is the right container for an array in modern c++ [duplicate]

This question already has answers here:
In which scenario do I use a particular STL container?
(10 answers)
Create a multidimensional array dynamically in C++
(1 answer)
Which is the fastest? A boost::multi_array or a std::vector?
(3 answers)
Closed 2 years ago.
I want to do some data-analysis, so I need at compile time unknown sized (large) arrays (of double or complex double). The number of dimensions is know at compile-time. What is the "right" way to do this in modern c++.

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.

vector of const ref to vector<vector<long>> assignment does not compile [duplicate]

This question already has answers here:
Why can't I make a vector of references?
(11 answers)
Closed 5 years ago.
We can't make a vector of references.
See already answered question.
vector doesn't support reference. you might use std::reference_wrapper or pointers instead.

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.

Why do we use pointers instead of local variables? [duplicate]

This question already has answers here:
Why use pointers? [closed]
(17 answers)
Closed 9 years ago.
I don't understand the difference between using a pointer and using a normal variable. I'm learning linked lists in class so the use of pointers seems more straightforward since pointers are used to go to the next node in the list, but I don't understand its more basic uses and I'm feeling stressed as it is something I should already understand but don't.
The pointer is in itself just a "normal" variable that just so happens to store a level of indirection to another variable.