多重集是類似於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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。