【发布时间】:2020-06-20 04:31:17
【问题描述】:
我正在尝试练习如何将一个地图元素移动到另一个 - 因此我尝试了以下代码:
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
}
输出:
Move element from one map to another
a= b=hello
s=1 m=1
为什么 std::map STL 容器的移动操作失败 - 我期待在 m['a'] 之后会为空?
【问题讨论】:
-
这能回答你的问题吗? What lasts after using std::move c++11