集是一种关联容器,其中每个元素都必须是唯一的,因为元素的值可以标识它。元素的值一旦添加到集合中就无法修改,尽管可以删除并添加该元素的修改后的值。
设置:: emplace()
仅当要插入的元素是唯一的并且在集合中尚不存在时,才可以使用此函数将新元素插入到集合容器中。
句法 :
setname.emplace(value)
Parameters :
The element to be inserted into the set
is passed as the parameter.
Result :
The parameter is added to the set if
the set does not contain that element already.
例子:
Input : myset{1, 2, 3, 4, 5};
myset.emplace(6);
Output : myset = 1, 2, 3, 4, 5, 6
Input : myset{1, 2, 3, 4, 5};
myset.emplace(4);
Output : myset = 1, 2, 3, 4, 5
错误和异常
1.它具有强大的异常保证,因此,如果引发异常,则不会进行任何更改
2.参数应与容器的类型相同,否则将引发错误
// INTEGER SET EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include
#include
using namespace std;
int main()
{
set myset{};
myset.emplace(2);
myset.emplace(6);
myset.emplace(8);
myset.emplace(9);
myset.emplace(0);
// set becomes 0, 2, 6, 8, 9
// adding unique element
myset.emplace(5);
// set becomes 0, 2, 5, 6, 8, 9
// adding element which already
// exists there will be no
// change in the set
myset.emplace(2);
// set remains 0, 2, 5, 6, 8, 9
// printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
输出:
0 2 5 6 8 9
// STRING SET EXAMPLE
// CPP program to illustrate
// Implementation of emplace() function
#include
#include
#include
using namespace std;
int main()
{
set myset{};
myset.emplace("This");
myset.emplace("is");
myset.emplace("a");
myset.emplace("computer science");
myset.emplace("portal");
// set becomes This, a, computer
// science, is, portal
// adding unique element
myset.emplace("GeeksForGeeks");
// set becomes GeeksForGeeks, This, is,
// a, computer science, portal
// adding element which already exists
// there will be no change in the set
myset.emplace("is");
// set remains GeeksForGeeks, This, is,
// a, computer science, portal
// printing the set
for (auto it = myset.begin();
it != myset.end(); ++it)
cout << ' ' << *it;
return 0;
}
输出:
GeeksForGeeks This a computer science is portal
时间复杂度: O(logn)
应用
使用emplace()函数输入一个具有以下数字和顺序的空多重集,然后找到元素的总和。
Input : 7, 9, 4, 6, 2, 5, 3
Output : 36
// CPP program to illustrate
// Application of emplace() function
#include
#include
using namespace std;
int main()
{
// sum variable declaration
int sum = 0;
// set declaration
set myset{};
myset.emplace(7);
myset.emplace(9);
myset.emplace(4);
myset.emplace(6);
myset.emplace(2);
myset.emplace(5);
myset.emplace(3);
// iterator declaration
set::iterator it;
// finding sum of elements
while (!myset.empty()) {
it = myset.begin();
sum = sum + *it;
myset.erase(it);
}
// printing the sum
cout << sum;
return 0;
}
输出 :
36
emplace()与插入
当使用insert时,我们创建一个对象,然后将其插入到多集合中。使用emplace(),可就地构建对象。
// C++ code to demonstrate difference between
// emplace and insert
#include
using namespace std;
int main()
{
// declaring set
set> ms;
// using emplace() to insert pair in-place
ms.emplace('a', 24);
// Below line would not compile
// ms.insert('b', 25);
// using emplace() to insert pair in-place
ms.insert(make_pair('b', 25));
// printing the set
for (auto it = ms.begin(); it != ms.end(); ++it)
cout << " " << (*it).first << " "
<< (*it).second << endl;
return 0;
}
输出 :
a 24
b 25
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。