亚洲区国产区激情区无码区,国产成人mv视频在线观看,国产A毛片AAAAAA,亚洲精品国产首次亮相在线

C++ 基礎(chǔ)教程

C++ 流程控制

C++ 函數(shù)

C++ 數(shù)組 & 字符串

C++ 數(shù)據(jù)結(jié)構(gòu)

C++ 類 & 對象

C++ 指針

C++ 繼承

C++ STL 教程

C++ 參考手冊

C++ set constructor(構(gòu)造函數(shù)) 使用方法及示例

C++ STL Set(集合)

set構(gòu)造函數(shù)有以下五種用途:

  1. 默認(rèn)構(gòu)造函數(shù):用于構(gòu)造具有零個(gè)元素的空set容器。

  2. 范圍構(gòu)造函數(shù):用于構(gòu)造內(nèi)容范圍為[first,last)的容器。

  3. 復(fù)制構(gòu)造函數(shù):用于構(gòu)造帶有現(xiàn)有容器元素副本的集合。

  4. move構(gòu)造函數(shù):用于使用move語義與其他元素一起構(gòu)造容器。

  5. 初始化程序列表構(gòu)造函數(shù):用于構(gòu)造帶有初始化程序列表內(nèi)容的集合。

語法

默認(rèn)構(gòu)造函數(shù)

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());	//到 C++ 11

explicit set (const key_compare& comp = key_compare(),
              const allocator_type& alloc = allocator_type());
explicit set (const allocator_type& alloc);			//從C ++ 11開始

范圍構(gòu)造器

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 ++ 11開始

復(fù)制構(gòu)造函數(shù)

set (const set& x);						//到 C++ 11
	
set (const set& x);
set (const set& x, const allocator_type& alloc);			//從C ++ 11開始

移動構(gòu)造函數(shù)

set (set&& x);
set (set&& x, const allocator_type& alloc);			//從C ++ 11開始

初始化列表構(gòu)造函數(shù)

set (initializer_list<value_type> il,
     const key_compare& comp = key_compare(),
     const allocator_type& alloc = allocator_type());		//從C ++ 11開始

參數(shù)

comp:比較函數(shù)對象,它接受兩個(gè)關(guān)鍵參數(shù),如果第一個(gè)參數(shù)在第二個(gè)參數(shù)之前,則返回true,否則返回false。默認(rèn)情況下,它使用less <key_type>謂詞。

alloc:一個(gè)分配器對象,用于此容器的所有內(nèi)存分配。

first:將迭代器輸入范圍內(nèi)的第一個(gè)位置。

last:將迭代器輸入到范圍中的最后一個(gè)位置。

x:另一個(gè)相同類型的set對象。

il:一個(gè)初始化器列表對象,將從中復(fù)制元素。

返回值

構(gòu)造函數(shù)從不返回任何值。

復(fù)雜度

對于空的構(gòu)造函數(shù)和移動的構(gòu)造函數(shù),復(fù)雜性將是恒定的。

對于所有其他情況,如果元素已經(jīng)排序,則迭代器之間的距離的復(fù)雜度將是線性的。

迭代器有效性

如果set容器的元素在move構(gòu)造函數(shù)中移動,則使與x相關(guān)的所有指針,迭代器和引用無效。

數(shù)據(jù)爭用

訪問所有復(fù)制的元素。

異常安全

萬一引發(fā)異常,則沒有任何效果。

實(shí)例1

讓我們看一下默認(rèn)構(gòu)造函數(shù)的簡單示例:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // 默認(rèn)構(gòu)造函數(shù)
   set<char> s;
  
   int size = s.size(); 

   cout << "集合s的大小 = " << size;
   return 0;
}

輸出:

集合s的大小 = 0

在上面的示例中,s是一個(gè)空集,因此size為0。

實(shí)例2

讓我們來看一個(gè)范圍構(gòu)造函數(shù)的簡單示例:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   int evens[] = {2,4,6,8,10};
  
   // 范圍構(gòu)造函數(shù)
   set<int> myset (evens, evens+5);  

   cout << "集合容器myset的大小為 : " << myset.size();
   return 0;
}

輸出:

集合容器myset的大小為: 5

在上面的示例中,set myset由evens元素構(gòu)成。

實(shí)例3

讓我們來看一個(gè)簡單的復(fù)制構(gòu)造函數(shù)示例:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   //默認(rèn)構(gòu)造函數(shù)
   std::set<int> s1;
   s1.insert(5);
   s1.insert(10);

   cout << "集合容器s1的大小為 : " << s1.size();
  
   // 復(fù)制構(gòu)造函數(shù)
   set<int> s2(s1);
   cout << "\n新集合容器s2的大小為 : " << s2.size();
   return 0;
}

輸出:

集合容器s1的大小為 : 2
新集合容器s2的大小為 : 2

在上面的示例中,s2是s1集合的拷貝副本。

實(shí)例4

我們來看一個(gè)簡單的移動構(gòu)造函數(shù)示例:

#include <iostream>
#include <set>

using namespace std;

int main(void) {
   // 默認(rèn)構(gòu)造函數(shù)
   set<char> s1;
   s1.insert('x');
   s1.insert('y');

   cout << "集合容器s1的大小為 : " << s1.size();

   // Move 構(gòu)造函數(shù)
   set<char> s2(move(s1));
   cout << "\n新集合容器s2的大小為 : " << s2.size();
   return 0;
}

輸出:

集合容器s1的大小為 : 2
新集合容器s2的大小為 : 2

在上面的示例中,s1的內(nèi)容被移至s2 set。

實(shí)例5

讓我們看一個(gè)簡單的初始化列表構(gòu)造函數(shù)示例:

#include <iostream>
#include <set>
#include <string>

using namespace std;

int main() {
   // 初始化列表構(gòu)造函數(shù)
   set<string> fruit {
      "orange", "apple", "mango", "peach", "grape"
   };

   cout << "容器內(nèi)fruit的大小為 : " << fruit.size();
   return 0;
}

輸出:

容器內(nèi)fruit的大小為 : 5

上面的示例創(chuàng)建一個(gè)以字符串為鍵的set水果,并使用initializer_list對其進(jìn)行初始化。

C++ STL Set(集合)