So my code works fine when I do not call the destructor of the vector class. But it bugs out and my vector bugs out when I call the vector destructor. Can anyone explain to me why? From what I understand, adding the destructor line should not make any difference as I am just freeing the object once I used finished them. I'm compiling it online on the geekforgeeks ide if it helps.
#include <iostream>
#include <vector>
using namespace std;
//Function that converts from base 10 to another base given by user input
//And results are stored in the vector
int getRepresent(vector<int> &vec, int base, int num) {
if (num < base) {
vec.push_back(num);
return 1;
}
vec.push_back(num % base);
return 1 + getRepresent(vec, base, num / base);
}
//Compute the sum of squares of each digit
int computeSsd(int base, int num) {
vector<int> vec;
int len = getRepresent(vec, base, num);
int i;
int sum = 0;
for (i = 0; i < len; i++) {
sum += vec[i] * vec[i];
}
/*
for (auto x: vec) {
cout << x <<' ';
}
vec.~vector(); //the weird part that cause my vector to change once i add this line
*/
return sum;
}
int main() {
int size;
int i;
cin >> size;
for (i = 0; i < size; i++) {
int display;
int base;
int num;
cin >> display >> base >> num;
int ssd = computeSsd(base, num);
cout << display << ' ' << ssd << '\n';
}
return 0;
}