1

我正在尝试使用迭代器擦除向量中的最后一个元素。但是我在擦除元素时遇到了分段错误。

下面是我的代码:

    for (vector<AccDetails>::iterator itr = accDetails.begin(); itr != accDetails.end(); ++itr) {
    if (username == itr->username) {
            itr = accDetails.erase(itr);
    }
}

我的迭代有问题吗?

4

2 回答 2

5

这是应用删除/擦除习语的好地方:

accDetails.erase(
    std::remove_if(
        accDetails.begin(), accDetails.end(), 
        [username](AccDetails const &a) { return username == a.username; }),
     accDetails.end());

作为奖励,这可能比你正在做的要快一点(或者如果你的向量很大,可能会快一点)。单独擦除每个项目最终结果为 O(N 2 ),但这将是 O(N),当/如果 N 变大时,这可能非常重要。

如果您不能使用 C++11,则 lambda 将不起作用,因此您需要单独对该比较进行编码:

class by_username { 
    std::string u;
public:
    by_username(std::string const &u) : u(u) {}
    bool operator()(AccDetails const &a) { 
        return u == a.username;
    }
};

accDetails.erase(
    std::remove_if(accDetails.begin(), accDetails.end(), by_username(username)), 
    accDetails.end());

operator==或者,您可以为您的AccDetails班级重载,并在那里处理比较。例如:

#include <vector>
#include <iostream>
#include <algorithm>
#include <string>
#include <iterator>

class AccDetail {
    std::string name;
    int other_stuff;
public:
    AccDetail(std::string const &a, int b) : name(a), other_stuff(b) {}

    bool operator==(std::string const &b) {
        return name == b;
    }

    friend std::ostream &operator<<(std::ostream &os, AccDetail const &a) {
        return os << a.name << ", " << a.other_stuff;
    }
};

int main(){
    std::vector<AccDetail> ad = { {"Jerry", 1}, { "Joe", 2 }, { "Bill", 3 } };

    std::cout << "Before Erase:\n";
    std::copy(ad.begin(), ad.end(), std::ostream_iterator<AccDetail>(std::cout, "\n"));
    ad.erase(
        std::remove(ad.begin(), ad.end(), "Joe"),
        ad.end());

    std::cout << "\nAfter Erasing Joe:\n";
    std::copy(ad.begin(), ad.end(), std::ostream_iterator<AccDetail>(std::cout, "\n"));
}
于 2013-08-03T04:35:14.110 回答
-2

我学会了一种安全的方法来清除我的领导者身上的元素。首先,找到所有元素。其次,将它们一一擦除。

queue< vector<AccDetails>::iterator > q;
for (vector<AccDetails>::iterator itr = accDetails.begin(); itr != accDetails.end(); ++itr) {
    if (username == itr->username) {
        //itr = accDetails.erase(itr);
        q.push(itr);
    }
}
while(!q.empty()){
    vector<AccDetails>::iterator itr = q.front();
    accDetails.erase(itr);
    q.pop();
}
于 2013-08-03T05:32:28.880 回答