实例化unordered_multiset

时间:2018-03-28 06:04:18

标签: c++ stl

我有一个名为magazine的字符串向量,我想将向量中的所有字符串插入到unordered_multiset中。我写了这样的代码。

unordered_multiset<string> magazine_set;

for (auto i = magazine.begin(); i != magazine.end(); i++){
    magazine_set.insert(*i);
}

是否有更短的方法可以做到这一点。?

2 个答案:

答案 0 :(得分:4)

您可以使用构造函数:

unordered_multiset<string> magazine_set(magazine.begin(), magazine.end());

答案 1 :(得分:1)

insert成员函数函数有一个重载,需要两个迭代器。

template< class InputIt >
void insert( InputIt first, InputIt last );

您可以使用:

unordered_multiset<string> magazine_set;
magazine_set.insert(magazine.begin(), magazine.end());