vector.erase(begin(vector))将删除vector的LAST元素

时间:2013-12-04 18:30:20

标签: c++ vector stl iterator erase

以下

void PlayListModel::removeTrack(int track) //does not work
{
    qDebug()<<track;
    assert(track >= 0);
    if (not mTracks.empty() and static_cast<int>(track) < mTracks.size())
    {
        auto iterator = begin(mTracks) + track;
        qDebug()<<iterator->getName();
        mTracks.erase(iterator);
        emit NeedRefreshView();
    }
}

将始终删除mTracks的最后一个元素(类型为std :: vector),尽管迭代器指向正确的元素。

我的AudioTrackModel运算符和构造函数:

AudioTrackModel::AudioTrackModel(const QString& path) :
mPath(path),
mFile(path)
{
    qDebug()<<"loaded file"<<" "<<path;
    TagHandler TagHandler(path);
    if (TagHandler.hasTags() == false)
    {
        mName = mFile.baseName();
    } else {
        storeName(TagHandler.getTitle());
        storeArtist(TagHandler.getArtist());
        mDiscNumber = TagHandler.getDisc();
        mTrackNumber = TagHandler.getTrack();
        storeAlbum(TagHandler.getAlbum());
        qDebug()<<"Track number is"<<mTrackNumber;
        qDebug()<<"Disc number is"<<mDiscNumber;
    }
    mDuration = TagHandler.getDuration();
}

AudioTrackModel::AudioTrackModel(AudioTrackModel&& other) :
mName(other.mName),
mPath(other.mPath),
mFile(other.mFile),
mDuration(other.mDuration),
mDiscNumber(other.mDiscNumber),
mAlbum(other.mAlbum),
mArtist(other.mArtist),
mTrackNumber(other.mTrackNumber),
mCurrent(other.mCurrent)
{
}

AudioTrackModel::AudioTrackModel(const AudioTrackModel& other) :
mName(other.mName),
mPath(other.mPath),
mFile(other.mFile),
mDuration(other.mDuration),
mDiscNumber(other.mDiscNumber),
mAlbum(other.mAlbum),
mArtist(other.mArtist),
mTrackNumber(other.mTrackNumber),
mCurrent(other.mCurrent)
{
}

AudioTrackModel& AudioTrackModel::operator=(AudioTrackModel&& other)
{
    mPath =other.mPath;
    mFile = other.mFile;
    mDuration = other.mDuration;
    mDiscNumber = other.mDiscNumber;
    mAlbum = other.mAlbum;
    mArtist = other.mArtist;
    mTrackNumber = other.mTrackNumber;
    mCurrent = other.mCurrent;
    return *this;
}

AudioTrackModel& AudioTrackModel::operator=(const AudioTrackModel& other)
{
    mPath =other.mPath;
    mFile = other.mFile;
    mDuration = other.mDuration;
    mDiscNumber = other.mDiscNumber;
    mAlbum = other.mAlbum;
    mArtist = other.mArtist;
    mTrackNumber = other.mTrackNumber;
    mCurrent = other.mCurrent;
    return *this;
}

这个问题让我发疯,我不知道发生了什么。

0 个答案:

没有答案