lundi 27 mars 2017

c++11 moving elements between list to map (or other containers)

Is there an easy way to move elements between different containers?
I couldn't find any simple way (using <algorithm>) to do the following:

Non copyable class

class NonCopyable {
public:
    NonCopyable() {};
    ~NonCopyable() {};
    NonCopyable(const NonCopyable&) = delete;
    NonCopyable& operator=(const NonCopyable&) = delete;
    NonCopyable(NonCopyable&& that) {}
};

Move operations:

std::list<NonCopyable> eList;
std::map<int, NonCopyable> eMap;

eList.push_back(NonCopyable());

// Move from list to map
{
    auto e = std::move(eList.back());
    eList.pop_back();
    eMap.insert(std::make_pair(1, std::move(e)));
}

// Move from map to list
{
    auto it = eMap.find(1);
    if (it != eMap.end()) {
        eList.push_back(std::move(it->second));
        auto e = eMap.erase(it);
    }
}

// Move all
// Iterate over map?...

I've seen std::list::splice but it won't help me here because I have a list and a map, and not two lists...

Thanks

Aucun commentaire:

Enregistrer un commentaire