У меня есть функция, в которой деструктор вектора вызывается немедленно и вызывает ошибку. Я видел это с помощью отладчика QtCreator
Но когда я закомментировал следующую строку, она работает (не так, как ожидалось, но, по крайней мере, не дает ошибок).
#include <algorithm>
#include <cmath>
#include <functional>
#include <iostream>
#include <vector>
class Solution {
private:
int min_cost(const std::vector<int>& costs, std::vector<int>& map, int start) const
{
// if (map[start] > -1)
// return map[start];
const int s = costs.size();
if (start >= s - 2)
return costs[start];
int c1 = min_cost(costs, map, start + 1);
int c2 = min_cost(costs, map, start + 2);
int cost = std::min(c1, c2);
if (start > -1)
cost += costs[start];
map[start] = cost;
return cost;
}
public:
int minCostClimbingStairs(const std::vector<int>& costs) const
{
std::vector<int> map(costs.size(), -1); // from debuder, std::vector<int>::~vector is called here
return min_cost(costs, map, -1); // commeting out this line out dont gives error
}
};
int main()
{
const std::vector<std::vector<int>> input = {
{ 10, 15 },
{ 10, 15, 20 },
{ 1, 100, 1, 1, 1, 100, 1, 1, 100, 1 },
{ 0, 0, 0, 0 }
};
const Solution Sol;
for (auto const& i : input) {
std::cout << ' ' << Sol.minCostClimbingStairs(i) << '\n';
}
}
Но Как функция min может повлиять на деструктор вектора? вот fiddle Я тестировал код с g ++ 10.1.0 и clang ++ 10.0.0