How to Copy an Array in C++

  1. Use the copy() Function to Copy an Array in C++
  2. Use the copy_backward() Function to Copy an Array
  3. Use the assign() Method to Copy an Array

This article will introduce how to copy an array in C++.

Use the copy() Function to Copy an Array in C++

The copy() method is the recommended way of copying range based structures with a single function call. copy() takes the first and the last elements of the range and the beginning of the destination array. Notice that you might get undefined behavior if the third parameter is within the source range.

#include <iostream>
#include <vector>
#include <iterator>
#include <string>

using std::cout; using std::endl;
using std::vector; using std::copy;
using std::string;

int main() {
    vector<string> str_vec = { "Warty", "Hoary",
                               "Breezy", "Dapper",
                               "Edgy", "Feisty" };
    vector<string> new_vec(str_vec.size());

    copy(str_vec.begin(), str_vec.end(), new_vec.begin());

    cout << "new_vec - | ";
    copy(new_vec.begin(), new_vec.end(),
         std::ostream_iterator<string>(cout," | "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

new_vec - | Warty | Hoary | Breezy | Dapper | Edgy | Feisty |

Use the copy_backward() Function to Copy an Array

The copy_backward() method can copy an array in reverse order with the original element, but the order is preserved. When copying the overlapping ranges, there is a rule of thumb one should mind when using the methods std::copy and std::copy_backward: copy is appropriate when copying to the left (beginning of the destination range is outside the source range). In contrast, copy_backward is appropriate when copying to the right (the end of the destination range is outside the source range).

#include <iostream>
#include <vector>
#include <iterator>
#include <string>

using std::cout;
using std::endl;
using std::vector;
using std::copy_backward;
using std::string;

int main() {
    vector<string> str_vec = { "Warty", "Hoary",
                               "Breezy", "Dapper",
                               "Edgy", "Feisty" };
    vector<string> new_vec(str_vec.size());

    copy_backward(str_vec.begin(), str_vec.end(), new_vec.end());

    cout << "new_vec - | ";
    copy(new_vec.begin(), new_vec.end(),
         std::ostream_iterator<string>(cout," | "));
    cout << endl;

    return EXIT_SUCCESS;
}

Use the assign() Method to Copy an Array

assign() is a builtin method of a vector container, which replaces the contents of the calling vector object with passed elements of the range. assign() method could be handy when copying the vectors of the types which can be converted to each other easily. In the following code example, we demonstrate a way to copy a char vector into the int vector with a single assign call.

#include <iostream>
#include <vector>
#include <iterator>
#include <string>

using std::cout;
using std::endl;
using std::vector;

int main() {
    vector<char> char_vec { 'a', 'b', 'c', 'd', 'e'};
    vector<int> new_vec(char_vec.size());

    new_vec.assign(char_vec.begin(), char_vec.end());

    cout << "new_vec - | ";
    copy(new_vec.begin(), new_vec.end(),
         std::ostream_iterator<int>(cout," | "));
    cout << endl;

    return EXIT_SUCCESS;
}

Output:

new_vec - | 97 | 98 | 99 | 100 | 101 |

Related Article - C++ Array

  • How to Clear Array Element Values in C++
  • How to Pass 2D Array to a Function in C++
  • comments powered by Disqus