BOOST :: INTRUSIVE Q4

时间:2015-03-22 16:23:14

标签: c++ c++11 boost intrusive-containers

以下程序并没有按照我的预期行事。执行后

values.erase(
            std::remove_if(begin(values), end(values), std::mem_fn(&MyClass::bIsMarkedToDelete))); 

我认为values并不包含任何&#34; Mike&#34;并且长度将是7或11 - 4.而不是&#34; Mike&#34;好像被添加到std::vector<MyClass> value的末尾,计数是10?

#include <vector>
#include <iostream>
#include <vector>
#include <functional>

#include <boost/intrusive/unordered_set.hpp>

namespace bic = boost::intrusive;

std::hash<std::string> hash_fn;

struct MyClass : bic::unordered_set_base_hook<bic::link_mode<bic::auto_unlink>>
{
    std::string name;
    int anInt1;
    mutable bool bIsMarkedToDelete;

    MyClass(std::string name, int i) : name(name), anInt1(i), bIsMarkedToDelete(false) {}

    bool operator==(MyClass const& o) const
    {
        //return anInt1 == o.anInt1 && name == o.name;
        return name == o.name;
    }

    struct hasher
    {
        size_t operator()(MyClass const& o) const
        {
            return o.anInt1;
            //return hash_fn(o.name);
        }
    };
};

typedef bic::unordered_set<MyClass, bic::hash<MyClass::hasher>, bic::constant_time_size<false> > HashTable;

int main()
{
    std::vector<MyClass> values
    {
        MyClass { "John", 0     },
        MyClass { "Mike",  1    },
        MyClass { "Dagobart", 2 },
        MyClass { "John", 3     },
        MyClass { "Mike",  4    },
        MyClass { "Dagobart", 5 },
        MyClass { "John", 6     },
        MyClass { "Mike",  7    },
        MyClass { "Dagobart", 8 },
        MyClass { "John", 9     },
        MyClass { "Mike", 10    }
    };

    HashTable::bucket_type buckets[100];
    HashTable hashtable(values.begin(), values.end(), HashTable::bucket_traits(buckets, 100));

    for(auto& e: values)
        std::cout << e.name << " ";

    std::cout << '\n';
    std::cout << "values size first " << values.size() << '\n';
    std::cout << "hash size fist " << hashtable.size() << '\n';

    for(auto& e: values)
        e.bIsMarkedToDelete |= ("Mike" == e.name);

    std::cout << "removing all with bIsMarkedToDelete";
    for(auto& e: values)
        if(e.bIsMarkedToDelete)
            std::cout << e.name << " ";

    std::cout << '\n';

    values.erase(
        std::remove_if(begin(values), end(values), std::mem_fn(&MyClass::bIsMarkedToDelete)));

    std::cout << "values size now " << values.size() << '\n';
    std::cout << "hash size now " << hashtable.size() << '\n';

    std::cout << "Contents of value after removing elements " << '\n';
    for(auto& e: values)
        std::cout << e.name << " ";

    std::cout << '\n';

    values.clear();

    std::cout << values.size() << '\n';
    std::cout << hashtable.size() << '\n';

    std::cout << "Done\n";

    int j;
    std::cin >> j;
}

编辑:

通过替换

进行修复
values.erase(
    std::remove_if(std::begin(values), std::end(values), std::mem_fn(&MyClass::bIsMarkedToDelete),
                   std::end(values)));

通过

for (auto it = values.begin();
          it != values.end();  /* nothing */)
{
    auto& e = *it;

    if(e.bIsMarkedToDelete)
    {
        //std::remove(e);

        it = values.erase(it);

        if(it == values.end())
            break;
    }
    else
    {
        //std::cout << "Provider not equal\n";
        ++it; // this is where we increment
    }
}

编辑2:

更好的是

values.erase(
    std::remove_if(std::begin(values), std::end(values), std::mem_fn(&MyClass::bIsMarkedToDelete)),
                   std::end(values));

0 个答案:

没有答案