我怎么会用的for_each删除一个STL地图的每一个值? [英] How would I use for_each to delete every value in an STL map?

查看:144
本文介绍了我怎么会用的for_each删除一个STL地图的每一个值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个STL地图,该值是三分球,我想将它们全部删除。我将如何重新present以下code,但利用的std :: for_each的呢?我很高兴的解决方案,使用升压。

 的(stdext ::的hash_map< INT,符*> ::迭代器IR = myMap.begin();
     IR = myMap.end()!;
     ++ IR)
{
  删除的IR>第二个; //删除所有(富*)值。
}
 

(我发现Boost的 checked_delete ,但我不知道如何应用到对< INT,符*> 迭代器重新presents)。

(另外,对于这个问题的目的,忽视的事实是存储需要删除的STL容器原始指针不是非常明智的)。

注意:我后来发现,下面列出一个单行的答案......但是code是pretty的可怕,所以我已经接受GMAN的理智的答案

解决方案

您必须做出一个函数对象:

 结构second_deleter
{
    模板< typename的T>
    void运算符()(const的T&安培; PX)常量
    {
        删除pX.second;
    }
};

的std :: for_each的(myMap.begin(),myMap.end(),second_deleter());
 

如果您使用的是刺激,你也可以使用λ文库:

 命名空间BL =提高::拉姆达;
的std :: for_each的(myMap.begin(),myMap.end(),second_deleter()
                BL ::绑定(BL :: delete_ptr()
                BL ::绑定(STD :: select2nd< MYMAP :: value_type的>(),_1));
 

但你可以尝试指针容器库,做到这一点自动。

请注意您不使用地图,但的hash_map 。我建议你​​切换至升压的 unordered_map ,这是更大的电流。然而,似乎没有成为一个 ptr_unordered_map

为了安全起见,你应该换这个东西了。例如:

 模板< typename的T,类型名的删除>
结构wrapped_container
{
    的typedef牛逼CONTAINER_TYPE;
    类型定义删除器deleter_type;

    wrapped_container(const的T&安培; pContainer):
    集装箱(pContainer)
    {}

    〜wrapped_container(无效)
    {
        的std :: for_each的(container.begin(),container.end(),deleter_type());
    }

    牛逼的容器;
};
 

和使用它像:

 的typedef wrapped_container<
            提高:: unordered_map< INT,符*>中second_deleter> my_container;

my_container.container ./* ... * /
 

这确保了无论什么时候,您的容器将通过一个删除器迭代。 (对于例外,例如。)

比较:

 的std ::矢量<为int *>伏;
v.push_back(新INT);

抛出漏!; //没有在矢量被删除

wrapped_container<的std ::矢量<为int *> >伏;
v.container.push_back(新INT);

抛出无泄漏!; // wrapped_container自毁,删除元素
 

Suppose I have a STL map where the values are pointers, and I want to delete them all. How would I represent the following code, but making use of std::for_each? I'm happy for solutions to use Boost.

for( stdext::hash_map<int, Foo *>::iterator ir = myMap.begin();
     ir != myMap.end();
     ++ir )
{
  delete ir->second; // delete all the (Foo *) values.
}

(I've found Boost's checked_delete, but I'm not sure how to apply that to the pair<int, Foo *> that the iterator represents).

(Also, for the purposes of this question, ignore the fact that storing raw pointers that need deleting in an STL container isn't very sensible).

Note: I have subsequently found and listed a one-line answer below... but the code is pretty awful so I've accepted GMan's saner answer.

解决方案

You have to make a function object:

struct second_deleter
{
    template <typename T>
    void operator()(const T& pX) const
    {
        delete pX.second;
    }
};

std::for_each(myMap.begin(), myMap.end(), second_deleter());

If you're using boost, you could also use the lambda library:

namespace bl = boost::lambda;
std::for_each(myMap.begin(), myMap.end(), second_deleter(),
                bl::bind(bl::delete_ptr(), 
                bl::bind(std::select2nd<myMap::value_type>(), _1));

But you might try the pointer containers library which does this automatically.

Note you are not using a map, but a hash_map. I recommend you switch to boost's unordered_map, which is more current. However, there doesn't seem to be a ptr_unordered_map.

For safety, you should wrap this thing up. For example:

template <typename T, typename Deleter>
struct wrapped_container
{
    typedef T container_type;
    typedef Deleter deleter_type;

    wrapped_container(const T& pContainer) :
    container(pContainer)
    {}

    ~wrapped_container(void)
    {
        std::for_each(container.begin(), container.end(), deleter_type());
    }

    T container;
};

And use it like:

typedef wrapped_container<
            boost::unordered_map<int, Foo*>, second_deleter> my_container;

my_container.container./* ... */

This ensures no matter what, your container will be iterated through with a deleter. (For exceptions, for example.)

Compare:

std::vector<int*> v;
v.push_back(new int);

throw "leaks!"; // nothing in vector is deleted

wrapped_container<std::vector<int*> > v;
v.container.push_back(new int);

throw "no leaks!"; // wrapped_container destructs, deletes elements

这篇关于我怎么会用的for_each删除一个STL地图的每一个值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆