std::copy
std::copy
is a powerful algorithm in the C++ Standard Library that efficiently copies a range of elements from one container to another. It's part of the <algorithm>
header and provides a flexible way to transfer data between different container types or within the same container. Let's explore various examples of std::copy
to understand its versatility and usage in different scenarios.
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(5);
std::copy(source.begin(), source.end(), destination.begin());
std::cout << "Destination vector: ";
for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
std::copy
is used to copy all elements from source
to destination
.#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
int main() {
std::vector<double> source = {1.1, 2.2, 3.3, 4.4, 5.5};
std::list<double> destination;
std::copy(source.begin(), source.end(), std::back_inserter(destination));
std::cout << "Destination list: ";
for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
vector
to a list
.std::back_inserter
as the output iterator, which allows std::copy
to add elements to the end of the destination container.#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<char> source = {'a', 'b', 'c', 'd', 'e', 'f'};
std::vector<char> destination(3);
std::copy(source.begin() + 2, source.begin() + 5, destination.begin());
std::cout << "Destination vector: ";
for (const auto& ch : destination) {
std::cout << ch << " ";
}
std::cout << std::endl;
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
int main() {
std::vector<std::string> words = {"Hello", "C++", "World"};
std::copy(words.begin(), words.end(),
std::ostream_iterator<std::string>(std::cout, " "));
std::cout << std::endl;
return 0;
}
std::copy
with std::ostream_iterator
to output directly to the console.std::ostream_iterator
is used as the destination, which writes each copied element to std::cout
.std::ostream_iterator
(" ") specifies a delimiter to be inserted between elements.#include <iostream>
#include <vector>
#include <algorithm>
class DoubleOutputIterator {
int* ptr;
public:
DoubleOutputIterator(int* p) : ptr(p) {}
DoubleOutputIterator& operator*() { return *this; }
DoubleOutputIterator& operator++() { ++ptr; return *this; }
DoubleOutputIterator operator++(int) { DoubleOutputIterator tmp = *this; ++ptr; return tmp; }
DoubleOutputIterator& operator=(int value) { *ptr = value * 2; return *this; }
};
int main() {
std::vector<int> source = {1, 2, 3, 4, 5};
std::vector<int> destination(5);
std::copy(source.begin(), source.end(), DoubleOutputIterator(destination.data()));
std::cout << "Destination vector (doubled values): ";
for (const auto& num : destination) {
std::cout << num << " ";
}
std::cout << std::endl;
return 0;
}
std::copy
with a custom output iterator.DoubleOutputIterator
, which doubles each value as it's being copied.std::copy
.std::copy
is a versatile algorithm in C++ that provides an efficient way to copy elements between containers or to other destinations. We've explored its basic usage with vectors, copying between different container types, copying subsets of elements, outputting directly to the console, and even using a custom output iterator for more complex operations.
Key points to remember:
1. std::copy
requires input iterators for the source range and an output iterator for the destination.
2. It can work with different container types and even with custom iterators.
3. When the destination size is known, pre-allocating space can improve performance.
4. For dynamic sizing, std::back_inserter
or similar inserters can be used.
5. std::copy
can be combined with other standard library features like std::ostream_iterator
for diverse applications.
Understanding and effectively using std::copy
can lead to more efficient and cleaner code when working with containers and data manipulation in C++.