‘=’是C++ STL中的一个操作符,它将无序的集合设置为另一个无序的集合,而unOrdEdj:St::Trase=相应的操作符函数。此函数有三个版本。
null
- 第一个版本将无序_集的引用作为参数,并将其复制到无序_集。
- 第二个版本执行移动分配,即将无序_集的内容移动到另一个无序_集。
- 第三个版本将初始值设定项列表的内容分配给无序的集合。
语法
uset.operator= ( unordered_set& us ) uset.operator= ( unordered_set&& us ) uset.operator= ( initializer list )
参数:
- 第一个版本将无序_集的引用作为参数。
- 第二个版本将无序_集的r值引用作为参数。
- 第三个版本将初始值设定项列表作为参数。
返回值: 它们都返回这个指针的值(*this)。
下面的程序说明了 无序集合::运算符= 在C++中。 节目:
// C++ code to illustrate the method // unordered_set::operator=() #include <iostream> #include <unordered_set> using namespace std; // merge function template < class T> T merge(T a, T b) { T t(a); t.insert(b.begin(), b.end()); return t; } int main() { unordered_set< int > sample1, sample2, sample3; // List initialization sample1 = { 7, 8, 9 }; sample2 = { 9, 10, 11, 12 }; // Merge both lists sample3 = merge(sample1, sample2); // copy assignment sample1 = sample3; // Print the unordered_set list for ( auto it = sample1.begin(); it != sample1.end(); ++it) cout << *it << " " ; cout << endl; for ( auto it = sample2.begin(); it != sample2.end(); ++it) cout << *it << " " ; cout << endl; for ( auto it = sample3.begin(); it != sample3.end(); ++it) cout << *it << " " ; cout << endl; } |
输出:
10 11 12 7 8 9 12 11 10 9 10 11 12 7 8 9
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END