C++ – How to add elements of a std::set to another non-empty set

c

  set<int> A, B;
  for (int i = 0; i < 100; i++) A.insert(i);
  for (int i = 50; i < 150; i++) B.insert(i);

How to add elements of set B to the set A?
In another words, how to implement A = Union(A, B)?

Best Answer

By default sets are unique values only and sorted so inserting the iterator range should work:

A.insert(B.begin(), B.end());