C++集合庫 - operator= 函式



描述

它將新的內容賦值給容器,替換其當前內容。

宣告

以下是std::set::operator=在不同C++版本中的工作方式。

C++98

set& operator= (const set& x);

C++11

set& operator= (const set& x);
set& operator= (set&& x);	
set& operator= (initializer_list<value_type> il)

返回值

它返回*this。

異常

如果丟擲異常,容器處於有效狀態。

時間複雜度

線性於容器的大小。

示例

以下示例顯示了std::set::operator=的用法。

#include <iostream>
#include <set>

int main () {
   int myints[] = { 10,20,30,40,50 };
   std::set<int> first (myints,myints+10);   
   std::set<int> second;                   

   second = first;                         
   first = std::set<int>();                

   std::cout << "Size of first: " << int (first.size()) << '\n';
   std::cout << "Size of second: " << int (second.size()) << '\n';
   return 0;
}

上述程式將正確編譯和執行。

Size of first: 0
Size of second: 8
set.htm
廣告