C++

C++ std::copy()

β€œWhen the source’s start and finish positions are specified, all the items in this range will be copied to the specified destination. The C++ STL provides several copy() variants that allow copy operations to be carried out in various ways, each with a unique use. All of these are described in the algorithm heading. These functions are explained in this article for use in regular programming.”

Example 1: Using copy() and copy_n() Method

copy(start_iter1, end_iter1, start_iter2) :The general copy function, copy(strat_iter1, end_iter, start_iter2), is used to move a cluster from one container to another. It needs three arguments:

start iter1: The starting point for copying elements is indicated by the pointer start iter, which points to the beginning of the source container.

end iter: The pointing device to the source container’s end, from which elements must be copied.

start iter2: The reference to the start of the target container, from which elements must be copied.

copy_n(start_iter1, num, start_iter2): The number of components that must be copied into the target container can be determined using this copy method. Additionally, it accepts the following three arguments:

start iter1: The identifier for the start of the source container, from which elements must be copied.

num: An integer indicating the number of numbers to be transferred to the destination container, starting with start iter1. When a negative number is provided, nothing happens.

start iter2: The destination container’s starting point, identified by the address start iter, is where items must start copying.

The code for this illustration is attached in the affixed screenshot.

In the example here, we have included three main libraries of C++. This library algorithm plays a vital role in this program as it provides access to the std::copy function. Then, we used the namespace std in the program. The using namespace declaration simply indicates that all objects in the std namespace should be available in the scope where it is present without requiring the std:: prefix.

After that, we have a program main method inside which we have declared a variable β€œvec_1” of a vector class and initialized it with the set of numeric values. We have also defined another two variables as β€œvec_2” and β€œvec_3”. We have declared these variables with the destination vectors. Then, we deployed the copy() method, which takes three arguments β€œvec_1.begin”, β€œvect_1.end”, and the β€œvect_2.begin” to copy the first two values of the vectors.

To print the copied vectors, we utilized the for loop, which cycled over each copied vector element and displayed the vector elements. Next, we have used the copy_n() method, which will display the first five elements of the vectors. For printing, the copied vector elements for loop are utilized as above.

The output generated by the copy() and copy_n() method is as follows:

Example 2: Using copy_if() and copy_backward() Method

copy_if(): As implied by the name, this function copies in accordance with the outcome of a “condition.” This is given with the aid of a function delivering a boolean value as the fourth argument. This function requires four arguments, three of which are identical to those in copy() and one additional function that, if it returns true, determines whether or not a number is duplicated.

copy_backward(): When using this function, items are copied into the target container backward until all of the numbers have not been copied. Start_iter2 is where the copying process begins, but it proceeds backward. It uses the same defenses as a copy(), too.

The code for this illustration is attached in the affixed screenshot.

In the first step of this program, we have inserted our libraries to fetch the C++ functions. Then, we declare the vector as v1 and initialize the vector with the integers. We have declared other variables as v2 and v3, which are the destination vectors. After that, we have the copy_if() function that only copies the odd numbers from the vectors. Next, the copy backward() is used to copy the first three elements, with the fourth ending position. We have printed the copied vector elements with the help of for loop.

The output generated by the copy_if() and copy_backward() method is as follows:

Example 3: Using inserter() Method

Let’s grasp the inserter() syntax before performing the copy() action.

# copy(strat_iter1, end_iter1, inserter(Container& x, typename Container::iterator it));

We utilize inserter() as the destination to which we wish to transfer the container’s elements. The inserter() needs two inputs. The first is an arbitrary-type container, while the second is an iterator inside the container.

It gives back an instance of insert iterator that is currently functioning with any sort of container. Using this wrapper function, insert iterator instances can be created. It can be difficult and hinder generic programming from having to know the exact entire type of the container when typing the name of the percent iterator. By using this function, you can benefit from automatic template parameter deduction and have the compiler automatically match the right types for you.

The code for this illustration is attached in the affixed screenshot.

Here, we have started with our main method. Inside it, we have declared the vector v1 and set the vectors with the numeric values. From the C++ iterator, we have created the object β€œitr”. Also, we have declared another variable, v2, in our program. Then, we invoked the insert() inside the copy function, which copies the vector elements through iteration. In the end, we have the for loop, which will print the copied elements of the specified vectors.

The output generated by the inserter() method is as follows:

Conclusion

In the article, we have discussed the std::copy functions that C++ supports. Four methods are demonstrated with the syntax and the example program. The parameters and functions of each approach differ. These techniques are accessible in the header file for the algorithm.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.