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.

enter image description here

  • To vskidku - call the method. Resize (), and then do push_back () - in the end - more elements than expected. - isnullxbh
  • temparyvector does not contain the resize method in the code - crystal
  • Yes, I indicate to you the general shortcomings. But with _myVector you are doing this. And in the loop, you do this 20 times: _temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]); _temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]); - isnullxbh
  • Just ask to pay attention to the code 3 of the designer, and the fact that you need to learn how to use the links)) - isnullxbh
  • one
    @crystal You have a task to create a dynamic vector class. This means that you should not use standard std :: vector containers in your class. So your decision is not originally correct. - Vlad from Moscow

2 answers 2

Your decision is not right. Your assignment says

Class - dynamic vector (one-dimensional array).

This means that your class should independently allocate memory dynamically for an array, change its size and delete the allocated memory.

You are not asked to create a wrapper around the standard container std::vector , since such a task does not make sense, since you can initially use this standard container without any wrappers as a dynamic array. And you are asked to create some simplified analogue of the class std::vector . These are completely different tasks.

This task is to test the ability to work with dynamic memory, and not to work with the standard class std::vector .

  • Yes thank you. I will redo, in this case. Thank! - crystal
  • @crystal The main thing is to write explicitly the correct copy constructor, assignment operator, and destructor. - Vlad from Moscow

Well, the second question is quite simple ...

  vector<int> _temparyVector(_vecSize * 2); for (int i = 0; i < _myVector.size(); i++) { _temparyVector.push_back(_myVector[i]); _temparyVector.push_back(_compareVector[i]); } 

You create a vector with _vecSize * 2 elements, and then you add 2*_myVector.size() elements to it ...

As for the _compareVector , then you pass it to the function by value ,

 void CompareAndSort(vector<int> _compareVector) 

which means that everything you do in the function with a copy does not affect the source vector at all ... That’s what it was before the call.

  • Thanks for solving the problem. Unfortunately, it is no longer relevant. Well, at least I know now at least something about vectors. Somehow it’s not nice to write to the CCP after Sharp: c - crystal