使用C ++中的条件擦除循环

时间:2016-07-13 16:15:48

标签: c++ loops erase erase-remove-idiom

有更好的写作方式:

for (auto i = container.begin(); i != container.end();)
{
    if (condition(i))
    {
       i = container.erase(i);
       continue;
    }
    ++i;
}

这段代码做了我想要的,但感觉就像是糟糕的风格。

我该如何改进?

我的容器是std::map,但通用的解决方案很酷。

3 个答案:

答案 0 :(得分:3)

使用erase + remove_if

auto pred = /* lambda or something*/
container.erase(std::remove_if(container.begin(),
                               container.end(),
                               pred)

答案 1 :(得分:0)

  

有没有更好的方法......?

它总是主观的,但一种方式是基于特征的模板函数套件,具有一致的界面,使用标签调度来根据容器类型选择最佳算法...

接口函数可能如下所示:

template<class Range, class Pred>
Range& erase_if(Range& range, Pred&& pred)
{
    erase_if(typename detail::range_traits<std::decay_t<Range>>::idiom_type(),
             range, std::forward<Pred>(pred));
    return range;
}

遵循容器类型的正确习惯用语......

void erase_if(erase_remove_idiom, Vector& vec, Pred pred)
{
    vec.erase(std::remove_if(std::begin(vec), std::end(vec),
                             std::forward<Pred>(pred)),
              std::end(vec));
}

template<class Maplike, class Pred>
void erase_if(equal_range_idiom, Maplike& map, Pred pred)
{
    auto first = std::begin(map);
    auto last = std::end(map);
    while (first != last) {
        auto& item = *first;
        auto& key = get_key(item);
        auto range = map.equal_range(key);
        if (pred(key)) {
            map.erase(range.first, range.second);
        }
        first = range.second;
    }
}

template<class Maplike, class Pred>
void erase_if(map_crawl_idiom, Maplike& map, Pred pred)
{
    for (auto i = map.begin(); i != map.end();)
    {
        i = pred(*i) ? map.erase(i) : std::next(i);
    }
}

这里是完整的代码和一些测试。

编写这样的代码总是让我对std-library维护者感到钦佩。这么多角落案件......

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <set>
#include <unordered_set>
#include <map>

namespace detail {

    // The general concept of range_traits
    template<class Range>
    struct range_traits
    {
    };

    // Tag for performing erase-remove on vector-like containers
    struct erase_remove_idiom {};

    // Using equal-range to skip redundant comparisons in multiset-like-containers
    struct equal_range_idiom {};

    // Crawling through maps...
    struct map_crawl_idiom {};

    template<class V, class A>
    struct range_traits<std::vector<V, A>> {
        using idiom_type = erase_remove_idiom;
    };

    template<class V, class C, class A>
    struct range_traits<std::multiset<V, C, A>> {
        using idiom_type = equal_range_idiom;
    };

    template<class V, class C, class A>
    struct range_traits<std::set<V, C, A>> {
        using idiom_type = map_crawl_idiom;
    };

    template<class V, class C, class H, class A>
    struct range_traits<std::unordered_set<V, C, H, A>> {
        using idiom_type = map_crawl_idiom;
    };

    template<class V, class C, class H, class A>
    struct range_traits<std::unordered_multiset<V, C, H, A>> {
        using idiom_type = equal_range_idiom;
    };

    template<class K, class V, class C, class A>
    struct range_traits<std::multimap<K, V, C, A>> {
        using idiom_type = map_crawl_idiom;
    };

    template<class K, class V, class C, class A>
    struct range_traits<std::map<K, V, C, A>> {
        using idiom_type = map_crawl_idiom;
    };
}


namespace detail {
    template<class Vector, class Pred>
    void erase_if(erase_remove_idiom, Vector& vec, Pred pred)
    {
        vec.erase(std::remove_if(std::begin(vec), std::end(vec),
                                 std::forward<Pred>(pred)),
                  std::end(vec));
    }

    // Generalised key-getter for sets
    template<class V>
    V& get_key(V& v) {
        return v;
    }

    // Specialised key-getter for maps
    template<class K, class V>
    const K& get_key(std::pair<const K, V>& p) {
        return p.first;
    }

    template<class Maplike, class Pred>
    void erase_if(equal_range_idiom, Maplike& map, Pred pred)
    {
        auto first = std::begin(map);
        auto last = std::end(map);
        while (first != last) {
            auto& item = *first;
            auto& key = get_key(item);
            auto range = map.equal_range(key);
            if (pred(key)) {
                map.erase(range.first, range.second);
            }
            first = range.second;
        }
    }

    template<class Maplike, class Pred>
    void erase_if(map_crawl_idiom, Maplike& map, Pred pred)
    {
        for (auto i = map.begin(); i != map.end();)
        {
            i = pred(*i) ? map.erase(i) : std::next(i);
        }
    }
}


//
// The interface function
//
template<class Range, class Pred>
Range& erase_if(Range& range, Pred&& pred)
{
    erase_if(typename detail::range_traits<std::decay_t<Range>>::idiom_type(),
             range, std::forward<Pred>(pred));
    return range;
}

template<class T>
struct emitter
{
    void operator()(std::ostream& os, const T& t) const {
        os << t;
    }
};

template<class K, class V>
struct emitter<std::pair<const K, V>>
{
    void operator()(std::ostream& os, const std::pair<const K, V>& p) const {
        os << "(" << p.first << ", " << p.second << ")";
    }
};

template<class Range>
void dump(Range& range)
{
    auto sep = "";
    auto e = emitter<typename std::decay_t<Range>::value_type> {};
    for (auto& item : range) {
        std::cout << sep;
        e(std::cout, item);
        sep = ", ";
    }
    std::cout << std::endl;
}

//
// Test on various containers
//
int main()
{
    std::vector<int> some { 1,0,1,0,1,0,1,0 };
    std::multiset<int> some_mset { 1,0,1,0,1,0,1,0 };
    std::unordered_multiset<int> some_umset { some_mset.begin(), some_mset.end() };
    std::map<int, std::string> some_map {
        { 1, "mouse" },
        { 2, "house" },
        { 3, "mouth" }
    };
    std::multimap<int, std::string> some_mmap(some_map.begin(), some_map.end());
    some_mmap.insert(some_map.begin(), some_map.end());

    std::set<int> some_set { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };


    auto is_one = [](auto& x) { return x == 1; };
    auto is_even = [](auto&x) { return (x % 2) == 0; };
    auto value_starts_with_m = [](auto& item) { return item.second.substr(0,1) == "m"; };

    dump(erase_if(some, is_one));
    dump(erase_if(some_mset, is_one));
    dump(erase_if(some_umset, is_one));
    dump(erase_if(some_map, value_starts_with_m));
    dump(erase_if(some_mmap, value_starts_with_m));
    dump(erase_if(some_set, is_even));
}

预期结果:

0, 0, 0, 0
0, 0, 0, 0
0, 0, 0, 0
(2, house)
(2, house), (2, house)
1, 3, 5, 7, 9

答案 2 :(得分:-2)

所有这些问题和评论都很古怪,但它们没有解决原始问题。

问的是,如何收紧以下内容:

  

for(i = ...; some_condition(i);){
  if(another_condition(i)){
  擦除(i);
  继续;
  }
   我++;
   }

答案是:

  

for(i = ...; some_condition(i); i ++){
  if(another_condition(i)){
  擦除(i);
  }
  }