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

C++ multiset constructor

C++ multiset constructor

multiset 构造函数有以下五种用途:
默认(空)构造函数: 用于构造一个零元素的空 multiset 容器。 范围构造器: 用于构造内容范围为[first,last)的容器。 复制构造函数: 该函数用于构造包含现有容器元素副本的 multiset 。 移动构造函数: 这用于通过移动语义与其他元素一起构造容器。 初始化器列表构造器: 这用于构造具有初始化器列表内容的 multiset 。

语法

默认构造函数
explicit multiset (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());              //until C++ 11
explicit multiset (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit multiset (const allocator_type& alloc);      //until C++ 14  
mutiset();
explicit multiset (const key_compare& comp,
                   const allocator_type& alloc = allocator_type());   //since C++ 14
explicit multiset (const allocator_type& alloc);
范围构造器
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& alloc = allocator_type());   //until C++ 11
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());     //until C++ 14  
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const key_compare& comp = key_compare(),
            const allocator_type& = allocator_type());
template <class InputIterator>
  multiset (InputIterator first, InputIterator last,
            const allocator_type& = allocator_type());    //since C++ 14
复制构造函数
multiset (const multiset& x);           //until C++ 11
  
multiset (const multiset& x);
multiset (const multiset& x, const allocator_type& alloc);      //since C++ 11
移动构造函数
multiset (multiset&& x);
multiset (multiset&& x, const allocator_type& alloc);     //since C++ 11
初始化列表构造器
multiset (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());   //until C++ 14
multiset (initializer_list<value_type> il,
          const key_compare& comp = key_compare(),        
          const allocator_type& alloc = allocator_type());
multiset (initializer_list<value_type> il,
          const allocator_type& alloc = allocator_type());    //since C++ 14

参数

comp : 比较函数对象,该对象具有两个关键参数,如果第一个参数位于第二个参数之前,则返回true,否则返回假。默认情况下,它使用less 谓词。
分配: 分配器对象用于此容器的所有内存分配。
第一: 将迭代器输入到第一个位置
last : 输入迭代器到范围的最后位置。
x : 的另一个 multiset 对象
il : 要从中复制元素的初始化列表对象。

返回值

构造函数从不返回任何值。

复杂度

对于空构造函数和移动构造函数,复杂度将保持不变。
对于在所有其他情况下,如果元素已经排序,则迭代器之间的距离复杂度将呈线性关系。

迭代器有效性

使所有指针,迭代器和引用无效如果 multiset 容器的元素在move构造函数中移动,则与x相关。

数据竞争

所有复制的元素都将被访问。

异常安全性

在引发异常的情况下没有影响。

示例1

让我们看看默认构造函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   // default constructor
   multiset<char> s;
  
   int size = s.size(); 
   cout << "Size of multiset s = " << size;
   return 0;
}
输出:
Size of multiset = 0
在上面的示例中,s是一个空的 multiset ,因此,大小为0。

示例2

让我们来看一个简单的范围构造函数示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   int evens[] = {2,6,6,8,10};
  
   // Range Constructor
   multiset<int> mymultiset (evens, evens+5);  
   cout << "Size of multiset container mymultiset is : " << mymultiset.size();
   return 0;
}
输出:
Size of multiset container mymultiset is: 5
在上面的示例中, multiset mymultiset是由偶数元素构成的。

示例3

让我们看一下复制构造函数的简单示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   //default Constructor
   multiset<int> s1;
   s1.insert(5);
   s1.insert(5);
   cout << "Size of multiset container s1 is : " << s1.size();
  
   // Copy constructor
   multiset<int> s2(s1);
   cout << "\nSize of new multiset container s2 is : " << s2.size();
   return 0;
}
输出:
Size of multiset container s1 is: 2
Size of new multiset container s2 is: 2
在上面的示例中,s2是s1 multiset 的副本。

示例4

让我们看一个简单的移动构造函数示例:
#include <iostream>
#include <set>
using namespace std;
int main(void) {
   // default constructor
   multiset<char> s1;
   s1.insert('x');
   s1.insert('y');
   s1.insert('y');
   cout << "Size of multiset container s1 is : " << s1.size();
   // Move constructor
   multiset<char> s2(move(s1));
   cout << "\nSize of new multiset container s2 is : " << s2.size();
   return 0;
}
输出:
Size of multiset container s1 is: 3
Size of new multiset container s2 is: 3
在上面的示例中,将s1的内容移至s2 multiset 。

示例5

让我们看一个简单的初始化列表构造函数示例:
#include <iostream>
#include <set>
#include <string>
using namespace std;
int main() {
   // Initializer list constructor
   multiset<string> fruit {"orange", "apple", "mango", "apple", "grape"};
   cout << "Size of multiset container fruit is: " << fruit.size();
   return 0;
}
输出:
Size of multiset container fruit is: 5
上面的示例创建一个以字符串为键的 multiset 水果,并使用initializer_list对其进行初始化。
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4