In C++, you can copy a set by creating a new set and inserting elements from the original set into the new one.
In this example,
originalSet
with some elements.copiedSet
.originalSet
and insert it into copiedSet
.#include <iostream>
#include <set>
int main() {
std::set<int> originalSet = {1, 2, 3, 4, 5};
std::set<int> copiedSet;
for (const auto& element : originalSet) {
copiedSet.insert(element);
}
// Print original and copied sets
std::cout << "Original Set:";
for (const auto& element : originalSet) {
std::cout << " " << element;
}
std::cout << std::endl;
std::cout << "Copied Set:";
for (const auto& element : copiedSet) {
std::cout << " " << element;
}
std::cout << std::endl;
return 0;
}
Original Set: 1 2 3 4 5 Copied Set: 1 2 3 4 5
In this example,
originalSet
.copiedSet
.#include <iostream>
#include <set>
int main() {
std::set<int> originalSet;
std::set<int> copiedSet;
// Print original and copied sets (both empty)
std::cout << "Original Set:";
for (const auto& element : originalSet) {
std::cout << " " << element;
}
std::cout << std::endl;
std::cout << "Copied Set:";
for (const auto& element : copiedSet) {
std::cout << " " << element;
}
std::cout << std::endl;
return 0;
}
Original Set: Copied Set:
In this tutorial, we learned How to Copy a Set in C++ language with well detailed examples.