💡 퀵 접속: cpp.kr/reverse_copy

reverse_copy

C++ 표준 라이브러리의 알고리즘으로, 주어진 범위의 요소들의 순서를 반대로 뒤집어서 다른 범위에 복사합니다. reverse_copy는 원본 범위를 수정하지 않고, 뒤집은 결과를 다른 범위에 복사합니다.

기본 사용법

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> numbers = {1, 2, 3, 4, 5};
    std::vector<int> result;
    
    std::cout << "원본 벡터: ";
    for (int n : numbers) std::cout << n << " ";
    std::cout << std::endl;
    
    // 벡터의 요소들을 반대로 뒤집어서 복사
    std::reverse_copy(numbers.begin(), numbers.end(),
                     std::back_inserter(result));
    
    std::cout << "뒤집어서 복사한 결과: ";
    for (int n : result) std::cout << n << " ";
    std::cout << std::endl;
    
    std::cout << "원본 벡터는 변경되지 않음: ";
    for (int n : numbers) std::cout << n << " ";
    std::cout << std::endl;
    
    return 0;
}

실행 결과:

원본 벡터: 1 2 3 4 5
뒤집어서 복사한 결과: 5 4 3 2 1
원본 벡터는 변경되지 않음: 1 2 3 4 5

문자열에서 사용

#include <iostream>
#include <string>
#include <algorithm>

int main() {
    std::string text = "Hello, World!";
    std::string result;
    
    std::cout << "원본 문자열: " << text << std::endl;
    
    // 문자열의 문자들을 반대로 뒤집어서 복사
    std::reverse_copy(text.begin(), text.end(),
                     std::back_inserter(result));
    
    std::cout << "뒤집어서 복사한 결과: " << result << std::endl;
    std::cout << "원본 문자열은 변경되지 않음: " << text << std::endl;
    
    return 0;
}

실행 결과:

원본 문자열: Hello, World!
뒤집어서 복사한 결과: !dlroW ,olleH
원본 문자열은 변경되지 않음: Hello, World!

사용자 정의 타입에서 사용

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

class Person {
    std::string name;
    int age;
public:
    Person(const std::string& n, int a) : name(n), age(a) {}
    
    void print() const {
        std::cout << name << " (" << age << "세)" << std::endl;
    }
};

int main() {
    std::vector<Person> people = {
        Person("Alice", 20),
        Person("Bob", 30),
        Person("Charlie", 25),
        Person("David", 35),
        Person("Eve", 22)
    };
    std::vector<Person> result;
    
    std::cout << "원본 목록:" << std::endl;
    for (const auto& person : people) {
        person.print();
    }
    
    // Person 객체들의 순서를 반대로 뒤집어서 복사
    std::reverse_copy(people.begin(), people.end(),
                     std::back_inserter(result));
    
    std::cout << "\n뒤집어서 복사한 결과:" << std::endl;
    for (const auto& person : result) {
        person.print();
    }
    
    std::cout << "\n원본 목록은 변경되지 않음:" << std::endl;
    for (const auto& person : people) {
        person.print();
    }
    
    return 0;
}

실행 결과:

원본 목록:
Alice (20세)
Bob (30세)
Charlie (25세)
David (35세)
Eve (22세)

뒤집어서 복사한 결과:
Eve (22세)
David (35세)
Charlie (25세)
Bob (30세)
Alice (20세)

원본 목록은 변경되지 않음:
Alice (20세)
Bob (30세)
Charlie (25세)
David (35세)
Eve (22세)

참고사항

  • reverse_copy는 algorithm 헤더에 정의되어 있습니다.
  • 원본 범위를 수정하지 않습니다.
  • 시간 복잡도는 O(n)입니다.
  • 요소들의 순서가 완전히 반대로 뒤집혀서 복사됩니다.
  • 요소들의 값은 변경되지 않고 순서만 변경되어 복사됩니다.
  • 대상 범위는 충분한 공간이 있어야 합니다.
  • back_inserter를 사용하면 대상 컨테이너가 자동으로 확장됩니다.
  • reverse는 원본 범위를 직접 수정하는 버전입니다.
함수 설명
reverse_copy(first, last, result) 범위 [first, last)의 요소들의 순서를 반대로 뒤집어서 result부터 시작하는 범위에 복사