vendredi 19 juin 2020

Why move on std::map is failing to move a element from one map to pther

I am trying to practice how to move one map element to other - hence I tried below code:

using namespace std;

int main(void)
{
    /* Initializer_list constructor */
    map<char, int> m =
    {
        {'a', 1},
        {'b', 2},
        {'c', 3},
        {'d', 4},
        {'e', 5},
    };

    cout << "Move element from one map to another" << endl;
    /*
    char temp;
    temp = map1[key1];
    map2[key1]=temp;
    map1.erase(key1)
    */
    string a = "hello";
    string b = move(a);
    cout << "a=" << a << " b=" << b << endl; // here string **a** is NULL as value is moved
    auto s = move(m['a']);
    cout << "s=" << s << " m=" << m['a'] <<  endl; // here 
} 

Output :

Move element from one map to another                                                                                        
a= b=hello                                                                                                              
s=1 m=1                    

Why move operation is failing for std::map STL container - I was expecting that after m['a'] would be empty?

Aucun commentaire:

Enregistrer un commentaire