I was reading some c++ code regarding binary functions and When i read the code, i didnt understand why there was a resize() function. Here is the code:
#include <vector>
#include <iostream>
using namespace std;
template <typename t="">
struct finddivision {
T operator() (const int& val, const int& val2) {
if (val > val2)
cout << val / val2 << endl;
else if (val2 > val)
cout << val2/val << endl;
}
};
int main() {
vector<int> myvec1 {2, 4, 5,6, 7,2};
vector<int> myvec2 {6, 16, 55, 36, 77, 22};
vector<int> holdresult;
holdresult.resize(myvec2.size());
transform(myvec1.begin(), myvec1.end(), myvec2.begin(),holdresult.begin(), finddivision<int>());
}
The line where it says: holdresult.resize(myvec2.size()), doesnt make sense to me, because aren't vectors already dynamic and adjust to the size they are given?. Another thing is that, when i comment that line and run the program, i get this return statement:
Process finished with exit code 139 (interrupted by signal 11: SIGSEGV)
So my questions is:
- Why do i need to have that resize() function for my code to work?
What I have tried:
i tried searching for answers and articles regarding my problem, but nothing seemed to match up.