0

我编写了一个函数,该函数应该从 MPEG 文件中删除所有标签,APIC 标签除外,但我得到了混合和不可预测的结果。有时,除了“年份”之外的所有标签都被正确删除(大多数情况下都会发生这种情况),有时,除了“年份”标签之外,还有一个或多个其他标签。

我肯定做错了什么。这是我的功能:

void stripTags(const char* path) {
    MPEG::File m(path);
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true); //I added this because otherwise, all tags would stay the first time I executed stripTags(). The second time I would execute it then the tags would be mostly gone (except for "year" as mentioned)
    ByteVector handle = "APIC";
    ID3v2::Tag *t = m.ID3v2Tag();
    if (t) {
        for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); it++) {
            if ((*it)->frameID() != handle) {
                t->removeFrames((*it)->frameID());
                it = t->frameList().begin(); //since the doc says that removeFrames invalidates the pointer returned by frameList, I update this pointer after each removal
            }
        }
        m.save();
    }
    m.strip(MPEG::File::ID3v1 | MPEG::File::APE, true);
}

谢谢您的帮助

4

1 回答 1

1

在循环中,当您删除标签时,您重置迭代器。然而,循环继续,它做的第一件事是it++,这意味着你的循环将跳过一个条目。

你可以修改你的循环,例如

for (ID3v2::FrameList::ConstIterator it = t->frameList().begin(); it != t->frameList().end(); /* nothing here */) {
    if ((*it)->frameID() != handle) {
        t->removeFrames((*it)->frameID());
        it = t->frameList().begin();
    } else {
        ++it;
    }
}
于 2015-09-21T08:36:42.463 回答