최대 1 분 소요

개요

  • 정의
    template< class T, class U = T >
    T exchange( T& obj, U&& new_value );
  • obj의 값을 new_value로 변경하고 obj의 값을 반환


예제

  • 코드
    #include <iostream>
    #include <utility>
    #include <vector>

    using namespace std;

    int main() {
    	int i = 1;
    	cout << exchange(i, 2) << ", " << i << endl;

    	cout << "------" << endl;

    	vector<int> v1{1, 2, 3};
    	auto v2 = exchange(v1, {5, 6, 7});

    	auto print = [](auto v) {
    		for (const auto &iter : v) {
    			cout << iter << " ";
    		}
    		cout << endl;
    	};

    	print(v1);
    	print(v2);

    	return 0;
    }
  • 실행 결과
    1, 2
    ------
    5 6 7
    1 2 3