C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ Set emplace_hint()

C++ Set emplace_hint()

C++ Set emplace_hint()函数用于通过使用提示将新元素插入容器来扩展set容器。元素的位置。元素是直接构建的(既不能复制也不能移动)。
通过给传递给该函数的参数args来调用元素的构造函数。
仅在没有key的情况下插入

语法

template <class.... Args>
    iterator emplace_hint (const_iterator position, Args&&... args);  //since C++ 11

参数

args: 传递来构造要插入到集合中的元素的参数。
position : 提示要插入新元素的位置。

返回值

它将迭代器返回到新插入的元素。如果元素已经存在,则插入失败,并将迭代器返回到现有元素。

复杂度

如果未指定位置,那么容器大小的复杂度将为对数
如果给出位置,则复杂度将摊销常量。

迭代器有效性

没有变化。

数据竞争

容器已修改。
尽管并发访问现有元素是安全的,但容器中的迭代范围并不安全。

异常安全

如果引发异常,则容器中没有任何变化。

示例1

让我们看看将元素插入集合的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   set<int> m = {60, 20, 30, 40};
            
   m.emplace_hint(m.end(), 50);
   m.emplace_hint(m.begin(), 10);
   cout << "Set contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< endl;
   return 0;
} 
输出:
Set contains following elements
10
20
30
40 
50
60
在上面的示例中,它只是将元素以给定位置的给定值插入集合m中。

示例2

一个简单的例子:
#include <set>  
#include <string>  
#include <iostream>  
  
using namespace std;  
  
template <typename M> void print(const M& m) {  
    cout << m.size() << " elements: " << endl;  
  
    for (const auto& p : m) {  
        cout << p << "  " ;  
    }  
  
    cout << endl;  
}  
  
int main()  
{  
    set<string> m1;  
  
    // Emplace some test data  
    m1.emplace("Ram");  
    m1.emplace("Rakesh");  
    m1.emplace("Sunil");  
  
    cout << "set starting data: ";  
    print(m1);  
    cout << endl;  
  
    // Emplace with hint  
    // m1.end() should be the "next" element after this emplacement  
    m1.emplace_hint(m1.end(), "Deep");  
  
    cout << "set modified, now contains ";  
    print(m1);  
    cout << endl;  
} 
输出:
set starting data: 3 elements: 
Rakesh  Ram  Sunil  
set modified, now contains 4 elements: 
Deep  Rakesh  Ram  Sunil

示例3

让我们看一个简单的示例,将元素插入到具有给定位置的集合中:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  set<char> myset;
  auto it = myset.end();
  it = myset.emplace_hint(it,'b');
  myset.emplace_hint(it,'a');
  myset.emplace_hint(myset.end(),'c');
  cout << "myset contains:";
  for (auto& x: myset)
    cout << " [" << x << ']';
    cout << '\n';
  return 0;
}
输出:
myset contains: [a] [b] [c]

示例4

让我们看一个插入元素的简单示例:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  typedef set<string> city;  
   string name;
   city fmly ;
   int n;
   cout<<"Enter the number of family members :";
   cin>>n;
   cout<<"Enter the name of each member: \n";
   for(int i =0; i<n; i++)
   {
       cin>> name;      // Get key
       fmly.emplace_hint(fmly.begin(),name);
       
   }
   
      cout<<"\nTotal memnbers in family are:"<< fmly.size();
      cout<<"\nDetails of family members: \n";
      cout<<"\nName \n ________________________\n";
      city::iterator p;
      for(p = fmly.begin(); p!=fmly.end(); p++)
      {
          cout<<(*p) <<" \n ";
      }
    
   return 0;
}
输出:
Enter the number of fmly members : 4
Enter the name of each member: 
Deep
Sonu
Ajeet
Bob
Total memnber of fmly is:4
Details of fmly members: 
Name 
 ________________________
 Ajeet 
 Bob 
 Deep 
 Sonu
在上面的示例中,它只是根据用户的选择将元素插入set的开头。

昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4