【发布时间】:2020-04-28 15:45:32
【问题描述】:
我有一段代码,在vector 中,元素是成对的int 和string。然后我想将所有元素从vector 移动到unordered_map<int, string>:
#include <algorithm>
#include <iostream>
#include <iterator>
#include <unordered_map>
#include <vector>
using namespace std;
template <typename C>
void print(const C& container) {
for (const auto& ele : container) {
cout << "(" << ele.first << ", " << ele.second << "), ";
}
cout << endl;
}
int main() {
vector<pair<int, string>> v {
{1, "one"},
{2, "two"},
{3, "three"},
{4, "four"},
{5, "five"}
};
unordered_map<int, string> uMap;
move(begin(v), end(v), inserter(uMap, begin(uMap)));
cout << "In unordered_map:" << endl;
print(uMap);
cout << endl << "In vector:" << endl;
print(v);
return 0;
}
我不明白的是结果:
In unordered_map:
(5, five), (4, four), (3, three), (2, two), (1, one),
In vector:
(1, ), (2, ), (3, ), (4, ), (5, ),
为什么那些整数留在vector 中?我以为move() 函数会将所有元素从vector 移动到unordered_map,这样vector 中就不会留下任何东西?
【问题讨论】:
标签: c++ vector move unordered-map