I have following code
#include <iostream>
#include <vector>
#include <functional>
int main() {
std::vector<double> v(5, 10.3);
std::vector<double>& r = v;
//std::vector<std::reference_wrapper<double>> r(v.begin(),v.end());
for (auto& i : v)
i *= 3;
for (auto i : r)
std::cout << i << " ";
std::cout << std::endl;
}
I am using reference of the vector 'r' using '&' operator and in the second line which I commented out I am using std::reference_wrapper of C++. Both do pretty much the same job? But I think there must be a purpose of making std::reference_wrapper even if we had '&' to do the job. can anyone explain please?
r.resize(200)
and you'll see the difference - one refers to the vector, others refers to thedouble
s. – Feathers&
here is not an operator. – Incorrect