我正在创建一个int的向量来存储一些值。 但是,下面的代码似乎根本不更新向量。 错误消息:
malloc: *** error for object 0x200000001: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
代码:
#include <vector>
#include <iostream>
#include "Q122.h"
void Q122::solve() {
std::vector<int> count(200, 0);
std::vector<std::vector<std::vector<int> > > wholelist(200);
count[0] = 0;
std::vector<int> one;
one.push_back(1);
wholelist[0].push_back(one);
int totalsum = 0;
int index = 0;
while(index != 199) {
int currentstep = count[index];
int currentpower = index+1;
std::vector<std::vector<int> > list = wholelist[index];
for(std::vector<int> each : list) {
for(int number : each) {
std::vector<int> copied(each);
int nextindex = number + currentpower - 1;
copied.push_back(nextindex + 1);
if(count[nextindex] == 0) {
count[nextindex] = currentstep + 1;
wholelist[nextindex].push_back(copied);
continue;
}
if(count[nextindex] != 0 && count[nextindex] > currentstep + 1) {
count[nextindex] = currentstep + 1;
wholelist[nextindex].clear();
wholelist[nextindex].push_back(copied);
continue;
}
if(count[nextindex] == currentstep + 1) {
wholelist[nextindex].push_back(copied);
continue;
}
}
}
std::cout << count[index] << std::endl;
index++;
}
int sum = 0;
for(int each : count)
sum += each;
std::cout << sum << std::endl;
}
此错误消息是什么意思?顺便问一下,如何检查矢量的内容?我在netbeans Mac OS上使用clang。
问题解决了:我忘了检查nextindex的界限。