这是我遇到问题的一小段代码。变位符函数返回正确的结果(带有变位符的字符串向量的向量)。除了最后一个元素为空之外,这个向量中充满了正确的变位符。因此,我尝试弹出它,但我总是收到一个"释放的指针未分配"错误
int main(int argc, const char * argv[]) {
Dictionary d = create_dictionary("dico.txt");
vector<vector<string> > v = anagrams("object", d, 0);
v.pop_back(); //Error here
return 0;
}
当涉及到"v.pop_back();"行时,会出现错误:"对象0x3000000003的malloc:***错误:未分配释放的指针"。
我想在pop_back之前发布内存的状态,但我不能发布图像,因为这是我的第一个问题,但以下是pop_back前v的内容:
v = {
[0] {
[0] "cob"
[1] "jet"
}
[1] {
[0] "object"
}
[2] { }
}
我找遍了所有能找的地方,什么也没找到=/有人知道它是从哪里来的吗?感谢您抽出时间;)
以下是我创建词典的方法:
Dictionary create_dictionary(const string& filename) {
Dictionary d;
//Read the file and store the words in d.words
ifstream file(filename.c_str());
string word;
if(file.is_open()) {
while (!file.eof()) {
file >> word;
d.words.push_back(word);
}
}
else
cout << "There is a problem with the file !" << endl;
file.close();
return d; }
如果v不为空,则调用pop_back,如:
if (!v.empty())
v.pop_back();