Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用迭代器c+删除对象类型的向量元素+;_C++_Algorithm_Object_Vector_Erase - Fatal编程技术网

C++ 使用迭代器c+删除对象类型的向量元素+;

C++ 使用迭代器c+删除对象类型的向量元素+;,c++,algorithm,object,vector,erase,C++,Algorithm,Object,Vector,Erase,我试图编写一个函数,根据向量元素的帐户名从listAccounts中删除向量元素。我写过这样的话: void Account::remove_account(string name) { auto iter = listAccounts.begin(); for ( ; iter != listAccounts.end(); iter++) { if ((*iter).account_name == name) { listAccou

我试图编写一个函数,根据向量元素的帐户名从listAccounts中删除向量元素。我写过这样的话:

void Account::remove_account(string name) {

    auto iter = listAccounts.begin();

    for ( ; iter !=  listAccounts.end(); iter++) {
        if ((*iter).account_name == name) {
            listAccounts.erase(iter);
        }
    }

}

但是我从向量删除中得到了一个分段错误,据我所知,这意味着我试图访问我无权访问的内存,但我不确定如何正确地写入它。

一旦删除迭代器指向的元素,迭代器将无效。(对于
std::vector
,删除元素后的所有其他迭代器也将无效)。递增或取消引用无效迭代器具有未定义的行为

您可以这样做(假设只删除一个元素):

对于多个元素,这将是:

void Account::remove_account(string name) {
    for(auto iter = listAccounts.begin(); iter != listAccounts.end(); ){
        iter = std::find_if(iter, listAccounts.end(),
                    [&](const auto& s){ return s.account_name == name; });
        if(iter != listAccounts.end())
            iter = listAccounts.erase(iter);  
    }
}

删除迭代器指向的元素后,该迭代器将无效。(对于
std::vector
,删除元素后的所有其他迭代器也将无效)。递增或取消引用无效迭代器具有未定义的行为

您可以这样做(假设只删除一个元素):

对于多个元素,这将是:

void Account::remove_account(string name) {
    for(auto iter = listAccounts.begin(); iter != listAccounts.end(); ){
        iter = std::find_if(iter, listAccounts.end(),
                    [&](const auto& s){ return s.account_name == name; });
        if(iter != listAccounts.end())
            iter = listAccounts.erase(iter);  
    }
}

如果只删除一个元素,则可以编写

bool Account::remove_account( std::string &name ) 
{
    auto it = std::find_if( listAccounts.begin(),
                            listAccounts.end(),
                            [&]( const auto &item )
                            {
                                return item.account_name == name;
                            } );

    bool success = it != listAccounts.end();

    if ( success ) listAccounts.erase( it );

    return success;
}
至于你的代码,在这句话之后

listAccounts.erase(iter);

迭代器无效。因此,您可以不增加它。

如果只删除一个元素,则可以编写

bool Account::remove_account( std::string &name ) 
{
    auto it = std::find_if( listAccounts.begin(),
                            listAccounts.end(),
                            [&]( const auto &item )
                            {
                                return item.account_name == name;
                            } );

    bool success = it != listAccounts.end();

    if ( success ) listAccounts.erase( it );

    return success;
}
至于你的代码,在这句话之后

listAccounts.erase(iter);

迭代器无效。所以您不能增加它。

如果修改了容器,迭代器将无效。 有两个很好的解决方案:

void Account::remove_account(const string& name) {
    auto iter = listAccounts.begin();

    while iter !=  listAccounts.end()) {
        if (iter->account_name == name) {
            iter = listAccounts.erase(iter);
        } else {
            ++iter;
        }
    }
}

// or
void Account::remove_account(const string& name) {
    listAccounts.erase(
        std::remove_if(std::begin(listAccounts), std::end(listAccounts),
                       [&name](const auto& item) {
                           return item.account_name == name;
                       }),
        std::end(listAccounts));
}

若修改了容器,迭代器将无效。 有两个很好的解决方案:

void Account::remove_account(const string& name) {
    auto iter = listAccounts.begin();

    while iter !=  listAccounts.end()) {
        if (iter->account_name == name) {
            iter = listAccounts.erase(iter);
        } else {
            ++iter;
        }
    }
}

// or
void Account::remove_account(const string& name) {
    listAccounts.erase(
        std::remove_if(std::begin(listAccounts), std::end(listAccounts),
                       [&name](const auto& item) {
                           return item.account_name == name;
                       }),
        std::end(listAccounts));
}

是否仅删除一个元素或具有给定值的所有元素?副本说明for循环在第一次擦除后无效。你需要休息一下在if语句中。@Vlad from Moscow在向量中只有一个元素的account.name等于name。@Giraffe船长,我明白了,谢谢。您想只删除一个元素还是删除具有给定值的所有元素?副本说明您的for循环在第一次擦除后无效。你需要休息一下。@Vlad来自莫斯科,向量中只有一个元素的account.name等于name。@我明白了,谢谢。在第二个解决方案中,您对向量调用
remove
。我想你的意思是
erase
。在第二个解决方案中,你在向量上调用
remove
。我想你的意思是删除。