多重集是类似于set的一种关联容器,不同之处在于多个元素可以具有相同的值。
multiset::emplace()
此函数用于将新元素插入到多集容器中。
用法:
multisetname.emplace(value) 参数: The element to be inserted into the multiset is passed as the parameter. Result: The parameter is added to the multiset.
例子:
Input :mymultiset{1, 2, 3, 4, 5}; mymultiset.emplace(6); Output:mymultiset = 1, 2, 3, 4, 5, 6 Input :mymultiset{}; mymultiset.emplace("This"); mymultiset.emplace("is"); mymultiset.emplace("Geeksforgeeks"); Output:mymultiset = This, is, Geeksforgeeks
错误和异常
1.它具有强大的异常保证,因此,如果引发异常,则不会进行任何更改
2.参数应与容器的类型相同,否则将引发错误
// INTEGER EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
multiset<int> mymultiset{};
mymultiset.emplace(1);
mymultiset.emplace(56);
mymultiset.emplace(4);
mymultiset.emplace(9);
mymultiset.emplace(0);
// multi set becomes 1, 56, 4, 9, 0
// adding another element
mymultiset.emplace(87);
// printing the multiset
for (auto it = mymultiset.begin();
it != mymultiset.end(); ++it)
cout << ' ' << *it;
return 0;
}
输出:
1 56 4 9 0 87
// STRING EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main()
{
multiset<string> mymultiset{};
mymultiset.emplace("This");
mymultiset.emplace("is");
mymultiset.emplace("a");
mymultiset.emplace("computer science");
mymultiset.emplace("portal");
// multi set becomes This, a,
// computer science, is, portal
// adding element
mymultiset.emplace("GeeksForGeeks");
// printing the multiset
for (auto it = mymultiset.begin();
it != mymultiset.end(); ++it)
cout << ' ' << *it;
return 0;
}
输出:
GeeksForGeeks This a computer science is portal
应用
使用emplace()函数输入具有以下数字和顺序的空多重集,并找到元素的总和。 emplace()优于插入的优点是,它避免了不必要的对象复制。
Input: 7, 9, 4, 6, 2, 5, 3 Output:36
// CPP program to illustrate
// Application of emplace() function
#include <iostream>
#include <set>
using namespace std;
int main()
{
// sum variable declaration
int sum = 0;
// multiset declaration
multiset<int> mymultiset{};
mymultiset.emplace(7);
mymultiset.emplace(9);
mymultiset.emplace(4);
mymultiset.emplace(6);
mymultiset.emplace(2);
mymultiset.emplace(5);
mymultiset.emplace(3);
// iterator declaration
set<int>::iterator it;
// finding sum of elements
while (!mymultiset.empty()) {
it = mymultiset.begin();
sum = sum + *it;
mymultiset.erase(it);
}
// printing the sum
cout << sum;
return 0;
}
输出:
36
时间复杂度:O(登录)
emplace()和insert()
当使用插入时,我们创建一个对象,然后将其插入到多集中。使用emplace(),该对象就地构建。
// C++ code to demonstrate difference between
// emplace and insert
#include<bits/stdc++.h>
using namespace std;
int main()
{
// declaring multiset of pairs
multiset<pair<char, int>> ms;
// using emplace() to insert pair in-place
ms.emplace('a', 24);
// Below line would not compile
// ms.insert('b', 25);
// using insert() to insert pair in-place
ms.insert(make_pair('b', 25));
// printing the multiset
for (auto it = ms.begin(); it != ms.end(); ++it)
cout << " " << (*it).first << " "
<< (*it).second << endl;
return 0;
}
输出:
a 24 b 25
有关详细信息,请参阅在std::map中插入元素(insert,emplace和operator [])。
相关用法
注:本文由纯净天空筛选整理自AyushSaxena大神的英文原创作品 multiset::emplace() in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。