Updated:

less than 1 minute read

개요

  • 복사가 아닌 이동되는 반복자
  • make_move_iterator 함수를 이용하여 생성


예제

  • 코드
     #include <iostream>
     #include <string>
     #include <vector>
        
     using namespace std;
        
     int main() {
     	auto print = [](const vector<string> &v) {
     		for (const auto &iter : v) {
     			cout << iter << " ";
     		}
     		cout << endl;
     	};
        
     	vector<string> v1{"a", "b", "c"};
        
     	vector<string> v2(v1.begin(), v1.end());
     	print(v2);
     	print(v1);
        
     	cout << "------" << endl;
        
     	vector<string> v3(make_move_iterator(v1.begin()),
     					  make_move_iterator(v1.end()));
     	print(v3);
     	print(v1);
     	cout << v1.size() << endl;
        
     	return 0;
     }
    
  • 실행 결과
     a b c
     a b c
     ------
     a b c
        
     3