C++에서 배열을 복사하는 방법

Jinku Hu 2023년10월12일
  1. copy()함수를 사용하여 C++에서 배열 복사
  2. copy_backward()함수를 사용하여 배열 복사
  3. assign()메서드를 사용하여 배열 복사
C++에서 배열을 복사하는 방법

이 기사에서는 C++로 배열을 복사하는 방법을 소개합니다.

copy()함수를 사용하여 C++에서 배열 복사

copy()메서드는 단일 함수 호출로 범위 기반 구조를 복사하는 데 권장되는 방법입니다. copy()는 범위의 첫 번째와 마지막 요소와 대상 배열의 시작을받습니다. 세 번째 매개 변수가 소스 범위 내에 있으면 정의되지 않은 동작이 발생할 수 있습니다.

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

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

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

출력:

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

copy_backward()함수를 사용하여 배열 복사

copy_backward()메서드는 원래 요소와 역순으로 배열을 복사 할 수 있지만 순서는 유지됩니다. 겹치는 범위를 복사 할 때std::copystd::copy_backward 메소드를 사용할 때 염두에 두어야 할 경험 법칙이 있습니다.copy는 왼쪽으로 복사 할 때 적합합니다 (대상 범위의 시작은 소스 범위 외부). 반면 copy_backward는 오른쪽으로 복사 할 때 적합합니다 (대상 범위의 끝이 소스 범위를 벗어남).

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

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

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

assign()메서드를 사용하여 배열 복사

assign()vector 컨테이너의 내장 메소드로 호출vector 객체의 내용을 범위의 전달 된 요소로 대체합니다. assign()메소드는 서로 쉽게 변환 할 수있는 유형의 벡터를 복사 할 때 편리 할 수 ​​있습니다. 다음 코드 예제에서는 단일 assign 호출로char 벡터를 int 벡터에 복사하는 방법을 보여줍니다.

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

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

출력:

new_vec - | 97 | 98 | 99 | 100 | 101 |
작가: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn Facebook

관련 문장 - C++ Array