I'm using C++17 after a long stint with C# and working through some Project Euler problems to get my feet well. Anyway, can anyone explain why createRandomVector(const int n) is not "moving" the vector created? I output the memory addresses and they only stay the same when passed by reference (obviously). Below is the code:
#include <iostream> #include <vector> #include <algorithm> #include <numeric> #include <random> using namespace std; auto createRandomVector(const int n) { vector<int> v(n); iota(begin(v), end(v), 1); shuffle(begin(v), end(v), std::mt19937()); cout << &v << endl; return v; } void printVector(const vector<int>& v, ostream& os); int main() { auto v = createRandomVector(100); printVector(v, cout); cout << endl; cout << &v << endl; return 0; } void printVector(const vector<int>& v, ostream& os) { cout << &v << endl; for_each(begin(v), end(v), [&os](auto& i) { os << i << " "; }); } Here is the output:
00BBFC20 00BBFD38 100 73 64 ... 85 90 00BBFD38 Why does the 1st memory address not match the 2nd? I have some understanding of how move works in modern C++ (a static_cast). But why is this not working?
&v[0]results in the same addresses. Obviously that makes senses and just missed that [minor] detail. Thank you!&vbut in Release mode I get the same.