std::copy


Function: 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.

Example 1: Basic Usage with Vectors

#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;
}

Explanation

Example 2: Copying Between Different Container Types

#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;
}

Explanation

Example 3: Copying a Subset of Elements

#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;
}

Explanation

Example 4: Copying to Console Output

#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;
}

Explanation

Example 5: Copying with a Custom Output Iterator

#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;
}

Explanation

Summary

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++.

Previous Page | Course Schedule | Course Content