Make Unreal REAL.
article thumbnail

 

std::replace() 함수를 이용해 컨테이너 범위 사이의 값을 변경할 수 있다.

 

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

using namespace std;

void print(vector<int> v);

void main()
{
    vector<int> v1{1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4};
    vector<int> v2;
    
    replace(v1.begin(), v1.end(), 1, 0);
    print(v1);
    
    replace_if(v1.begin(), v1.end(), [](int e) { return e == 2; }, 0);
    print(v1);
    
    replace_copy(v1.begin(), v1.end(), back_inserter(v2), 3, 0);
    print(v2);
    
    replace_copy_if(v2.begin(), v2.end(), v2.begin(), [](int e) { return e == 4; }, 0);
    print(v2);
}

 

출력

0 0 0 2 2 2 3 3 3 4 4 4

0 0 0 0 0 0 3 3 3 4 4 4

0 0 0 0 0 0 0 0 0 4 4 4

0 0 0 0 0 0 0 0 0 0 0 0
profile

Make Unreal REAL.

@diesuki4

포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!

검색 태그