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

C++ multiset emplace_hint()

C++ multiset emplace_hint()

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

语法

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

参数

args: 为构造要插入到容器中的元素而转发的参数。
position : 提示要插入新元素的位置。

返回值

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

复杂度

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

迭代器有效性

没有变化。

数据竞争

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

异常安全性

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

示例1

让我们看看将元素插入 multiset 中的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   multiset<int> m = {30, 20, 30, 10};
            
   m.emplace_hint(m.end(), 40);
   m.emplace_hint(m.begin(), 20);
   cout << "Multiset contains following elements" << endl;
   for (auto it = m.begin(); it != m.end(); ++it)
      cout << *it<< endl;
   return 0;
}
输出:
Multiset contains following elements
10
20
20
30
30
40
在上面的示例中,它只是将元素插入具有给定位置的给定值的 multiset 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()  
{  
    multiset<string> m1;  
  
    // Emplace some test data  
    m1.emplace("Ram");  
    m1.emplace("Deep");  
    m1.emplace("Sunil");  
  
    cout << "multiset 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 << "multiset modified, now contains ";  
    print(m1);  
    cout << endl;  
return 0;
}
输出:
multiset starting data: 3 elements: 
Deep  Ram  Sunil  
multiset modified, now contains 4 elements: 
Deep  Deep  Ram  Sunil

示例3

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

示例4

让我们看一个插入元素的简单示例:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
  typedef multiset<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 members of family is:"<< 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
在上面的示例中,它只是根据用户的选择将元素插入 multiset 的开头。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4