I received the following assignment at the university:
Create 2 objects of the developed class. Class - dynamic vector (one-dimensional array). As a result of the program execution, in the first object all even numbers should be contained, and in the second all odd numbers of the initial vectors. The contents of the objects (their vectors) before and after the exchange display.
My code is:
#include "stdafx.h" #include <vector> #include <iostream> #include <Windows.h> #include <algorithm> #include <ctime> using namespace std; class MyVectorClass { private: const int _vecSize = 10; vector<int> _myVector; vector<int>::iterator it; public: MyVectorClass() { _myVector.resize(_vecSize); } MyVectorClass(int _newVecSize) { _myVector.resize(_newVecSize); } MyVectorClass(vector<int> _copyVector) { _copyVector.resize(_vecSize); for (int i = 0; i < _copyVector.size(); i++) { _copyVector[i] = _myVector[i]; } } std::vector<int> GetVector() { return _myVector; } /*int GetItemByID(int id) { return _myVector[id]; } */ void GenerateData(int _params) { for (int i = 0; i < _myVector.size(); i++) { _myVector[i] = rand() % +_params; } } void CompareAndSort(vector<int> _compareVector) { vector<int> _temparyVector(_vecSize * 2); for (int i = 0; i < _myVector.size(); i++) { _temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]); } _myVector.clear(); _compareVector.clear(); int _trueCount = 0; int _falseCount = 0; for (int i = 0; i < _temparyVector.size(); i++) { if (_temparyVector[i] % 2 == 0) _trueCount++; } _falseCount = _temparyVector.size() - _trueCount; _myVector.resize(_trueCount); _compareVector.resize(_falseCount); for (int i = 0; i < _temparyVector.size(); i++) if (_temparyVector[i] % 2 == 0) _myVector.push_back(_temparyVector[i]); else _compareVector.push_back(_temparyVector[i]); } }; void CoutVectorData(vector<int> _targetVector) { for (int i = 0; i < _targetVector.size(); i++) { cout << _targetVector[i] << ' '; } cout << " " << endl; } int main() { srand(time(0)); MyVectorClass vec1; MyVectorClass vec2; vec1.GenerateData(100); vec2.GenerateData(100); cout << "First vector: "; CoutVectorData(vec1.GetVector()); cout << "Second vector: "; CoutVectorData(vec2.GetVector()); cout << "Compare and sort divided by 2:" << endl; vec1.CompareAndSort(vec2.GetVector()); cout << "First vector: "; CoutVectorData(vec1.GetVector()); cout << "Second vector: "; CoutVectorData(vec2.GetVector()); cout << "Say hello to cpp:"; system("pause"); return 0; } The code is far from decent, please forgive me. The question is: In the CompareAndSort function , why in the _compareVector are the even values ​​of the elements after the check?
Also, why is _temparyVector created with 40 (!) Elements instead of 20 (_vecsize * 2)?
Please tell me what I'm doing wrong.

_temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]);_temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]);- isnullxbh