unordered_set::emplace_hint()函數是C++ STL中的內置函數,僅當要插入的值唯一且具有給定提示時,才在unordered_set中插入新元素。
用法:
unordered_set_name.emplace_hint( position, value )
參數:該函數接受上述和以下描述的兩個參數:
- position:此參數用於描述插入操作的位置。
- value:此參數用於保存需要插入的內容。
返回值:如果unordered_set中不存在該值,則該函數將插入該值並返回一個指向插入元素的迭代器。否則,如果unordered_set中已經存在該值,則該函數返回指向該元素的迭代器。
以下示例程序旨在說明C++ STL中的unordered_set::emplace_hint()函數:
示例1:
// CPP program to illustrate
// unordered_set::emplace_hint() function
#include <iostream>
#include <unordered_set>
using namespace std;
// main program
int main()
{
// Initialize an unordered_set
unordered_set<int> uset = { 20, 40, 50, 60 };
// Insert an element that is not present
uset.emplace_hint(uset.begin(), 80);
// Display uset
cout << "uset: ";
for (auto it = uset.begin(); it != uset.end(); it++)
cout << *it << " ";
}
輸出:
uset: 80 20 40 50 60
示例2:
// CPP program to illustrate
// unordered_set::emplace_hint() function
#include <iostream>
#include <unordered_set>
using namespace std;
// main program
int main()
{
// Initialize an unordered_set
unordered_set<int> uset = { 20, 40, 50, 60 };
// Try to Insert an element that is not present
uset.emplace_hint(uset.begin(), 50);
// Display uset
cout << "uset: ";
for (auto it = uset.begin(); it != uset.end(); it++)
cout << *it << " ";
}
輸出:
uset: 60 50 40 20
相關用法
- C++ log()用法及代碼示例
- C++ div()用法及代碼示例
- C++ fma()用法及代碼示例
- C++ map key_comp()用法及代碼示例
- C++ wcsncpy()用法及代碼示例
- C++ imag()用法及代碼示例
- C++ real()用法及代碼示例
注:本文由純淨天空篩選整理自tufan_gupta2000大神的英文原創作品 unordered_set emplace_hint() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。