目录

范围构造函数(Range constructor)

描述 (Description)

C ++构造函数std::set::set() (Range Constructor)使用[first,last]范围中提到的元素构造一个set容器,每个set元素都是从该范围内的相应元素构造的。

声明 (Declaration)

以下是std :: set header中std :: set :: set()范围构造函数的声明。

C++98

template <class InputIterator>
 set (InputIterator first, InputIterator last,
      const key_compare& comp = key_compare(),
      const allocator_type& alloc = allocator_type());

C++11

template <class InputIterator>
   set (InputIterator first, InputIterator last,
        const key_compare& comp = key_compare(),
        const allocator_type& = allocator_type());

C++14

template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const key_compare& comp = key_compare(),
       const allocator_type& = allocator_type());
template <class InputIterator>
  set (InputIterator first, InputIterator last,
       const allocator_type& = allocator_type());

参数 (Parameters)

  • alloc - 将迭代器输入到初始位置。

  • comp - 用于所有键比较的比较函数对象

  • first, last - 要从中复制输入迭代器的范围。 此范围包括从第一个到最后一个的元素,包括首先指向的元素,但不包括最后指向的元素。

返回值

构造函数永远不会返回任何值。

异常 (Exceptions)

如果抛出任何异常,此成员函数不起作用。 但是,如果[first,last]指定的范围无效,则可能导致未定义的行为。

时间复杂

N log(N),其中N = std :: distance(first,last);

如果元素已经排序,则迭代器之间距离为线性(O(N))。

例子 (Example)

以下示例显示了std :: set :: set()范围构造函数的用法。

#include <iostream>
#include <set>
using namespace std;
int main(void) {
   char vowels[] = {'a','e','i','o','u'};
   // Range Constructor
   std::set<char> t_set (vowels, vowels+5);  
   std::cout <> "Size of set container t_set is : " << t_set.size();
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Size of set container t_set is : 5
↑回到顶部↑
WIKI教程 @2018