C++ STL 中的 multiset emplace_hint() 函数
在本文中,我们将讨论 C++ STL 中 multiset::emplace_hint() 函数的工作原理、语法和示例。
什么是 C++ STL 中的 multiset?
Multiset 与 set 容器类似,这意味着它们以键的形式存储值,就像 set 一样,并按照特定的顺序存储。
在 multiset 中,值与 set 一样被识别为键。multiset 和 set 之间的主要区别在于,set 具有不同的键,这意味着没有两个键是相同的,而在 multiset 中,可以存在相同的键值。
Multiset 键用于实现二叉搜索树。
什么是 multiset::emplace_hint()?
multiset::emplace_hint() 函数是 C++ STL 中的内置函数,它在 <set> 头文件中定义。此函数用于在关联的 multiset 容器中插入一个带有提示的新元素。
提示用于告知函数我们希望在何处放置新元素,然后它会在相应位置构造并插入该元素。此函数将容器的大小增加 1。
语法
ms_name.emplace_hint(const_iterator position, args&& val);
参数
该函数接受以下参数:
- position - 我们希望放置元素的提示位置。
- val - 我们希望在指定位置之后构造和放置的值。
返回值
此函数返回指向已放置元素的迭代器。
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
multiset<int> check;
auto i = check.emplace_hint(check.begin(), 4);
i = check.emplace_hint(i, 1);
check.emplace_hint(i, 9);
check.emplace_hint(i, 10);
cout<<"Elements are : ";
for (auto i = check.begin(); i!= check.end(); i++)
cout << *i << " ";
return 0;
}输出
如果我们运行以上代码,它将生成以下输出:
Elements are : 1 4 9 10
示例
#include <bits/stdc++.h>
using namespace std;
int main() {
multiset<int> check;
auto i = check.emplace_hint(check.begin(), 45);
i = check.emplace_hint(i, 40);
check.emplace_hint(i, 42);
check.emplace_hint(i, 30);
check.emplace_hint(check.begin(), 61);
check.emplace_hint(check.begin(), 6);
check.emplace_hint(check.begin(), 36);
cout<<"List is : ";
for (auto i = check.begin(); i != check.end(); i++)
cout << *i << " ";
return 0;
}输出
如果我们运行以上代码,它将生成以下输出:
Elements are : 6 30 36 40 42 45 61
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP