remove_if 等效于 std::map

2023-01-21C/C++开发问题
5

本文介绍了remove_if 等效于 std::map的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我试图根据特定条件从地图中删除一系列元素.我如何使用 STL 算法做到这一点?

I was trying to erase a range of elements from map based on particular condition. How do I do it using STL algorithms?

最初我想使用 remove_if 但这是不可能的,因为 remove_if 不适用于关联容器.

Initially I thought of using remove_if but it is not possible as remove_if does not work for associative container.

是否有任何适用于地图的remove_if"等效算法?

Is there any "remove_if" equivalent algorithm which works for map ?

作为一个简单的选择,我想到了遍历地图并擦除.但是循环遍历地图并擦除安全选项吗?(因为擦除后迭代器无效)

As a simple option, I thought of looping through the map and erase. But is looping through the map and erasing a safe option?(as iterators get invalid after erase)

我使用了以下示例:

bool predicate(const std::pair<int,std::string>& x)
{
    return x.first > 2;
}

int main(void) 
{

    std::map<int, std::string> aMap;

    aMap[2] = "two";
    aMap[3] = "three";
    aMap[4] = "four";
    aMap[5] = "five";
    aMap[6] = "six";

//      does not work, an error
//  std::remove_if(aMap.begin(), aMap.end(), predicate);

    std::map<int, std::string>::iterator iter = aMap.begin();
    std::map<int, std::string>::iterator endIter = aMap.end();

    for(; iter != endIter; ++iter)
    {
            if(Some Condition)
            {
                            // is it safe ?
                aMap.erase(iter++);
            }
    }

    return 0;
}

推荐答案

差不多.

for(; iter != endIter; ) {
     if (Some Condition) {
          iter = aMap.erase(iter);
     } else {
          ++iter;
     }
}

如果您确实从中删除了一个元素,那么您最初拥有的会增加迭代器两次;您可能会跳过需要删除的元素.

What you had originally would increment the iterator twice if you did erase an element from it; you could potentially skip over elements that needed to be erased.

这是我在很多地方看到使用和记录的常用算法.

This is a common algorithm I've seen used and documented in many places.

您是正确的,迭代器在擦除后失效,但只有引用被擦除元素的迭代器,其他迭代器仍然有效.因此在 erase() 调用中使用 iter++.

You are correct that iterators are invalidated after an erase, but only iterators referencing the element that is erased, other iterators are still valid. Hence using iter++ in the erase() call.

这篇关于remove_if 等效于 std::map的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

无法访问 C++ std::set 中对象的非常量成员函数
Unable to access non-const member functions of objects in C++ std::set(无法访问 C++ std::set 中对象的非常量成员函数)...
2024-08-14 C/C++开发问题
17

从 lambda 构造 std::function 参数
Constructing std::function argument from lambda(从 lambda 构造 std::function 参数)...
2024-08-14 C/C++开发问题
25

STL BigInt 类实现
STL BigInt class implementation(STL BigInt 类实现)...
2024-08-14 C/C++开发问题
3

使用 std::atomic 和 std::condition_variable 同步不可靠
Sync is unreliable using std::atomic and std::condition_variable(使用 std::atomic 和 std::condition_variable 同步不可靠)...
2024-08-14 C/C++开发问题
17

在 STL 中将列表元素移动到末尾
Move list element to the end in STL(在 STL 中将列表元素移动到末尾)...
2024-08-14 C/C++开发问题
9

为什么禁止对存储在 STL 容器中的类重载 operator&amp;()?
Why is overloading operatoramp;() prohibited for classes stored in STL containers?(为什么禁止对存储在 STL 容器中的类重载 operatoramp;()?)...
2024-08-14 C/C++开发问题
6