C++でのVectorについて、**=(等式)**でのコピーはどのようなコピーになっているのか?
という質問です。
実行環境は、
C++17、gcc、VScodeのcode runnerを用いています。
以下のコードを試し、sortを行ったところ、Vectorのコピーの仕方で違いが出たのでわかる方いましたら説明していただきたいです。
Cpp
1 vector<int> P = {1, 2, 3}; 2 int n = P.size(); 3 vector<int> Q(n + 2, 0), R(n + 2, 0); 4 Q = P; 5 6 for (int i = 0; i < n; i++) 7 { 8 R[i] = P[i]; 9 } 10 11 cout << "&Q : " << (size_t)&Q << endl; 12 cout << "&P : " << (size_t)&P << endl; 13 cout << "&R : " << (size_t)&R << endl; 14 15 sort(Q.begin(), Q.end()); 16 sort(R.begin(), R.end()); 17 cout << endl; 18 19 cout << "Q = Pで要素をコピーした場合" << endl; 20 for (int i = 0; i < n + 2; i++) 21 { 22 cout << Q[i] << ' '; 23 } 24 cout << endl; 25 cout << endl; 26 27 cout << "Q[i] = R[i]で要素をコピーした場合" << endl; 28 29 for (int i = 0; i < n + 2; i++) 30 { 31 cout << R[i] << ' '; 32 } 33 cout << endl;
cpp
1 &Q : 140732754266592 2 &P : 140732754266560 3 &R : 140732754266624 4 5 Q = Pで要素をコピーした場合 6 1 2 3 0 0 7 8 Q[i] = R[i]で要素をコピーした場合 9 0 0 1 2 3
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2020/04/24 02:31