STL - Algorithm/Algorithm - Modifying
Algorithm - replace_copy
Programmer.
2017. 7. 12. 11:19
원형)
1 2 3 4 | template <class InputIterator, class OutputIterator, class T> OutputIterator replace_copy (InputIterator first, InputIterator last, OutputIterator result, const T& old_value, const T& new_value); | cs |
의미)
[Iterator first, Iterator last)사이의 원소들중에 old_value값과 같은 것이 있으면 new_value로 교체한 다음 Iterator result 위치부터 복사합니다.
소스)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #include<iostream> #include<algorithm> #include<vector> void Print(const std::vector<int>& target) { for (std::vector<int>::const_iterator iterPos = target.begin(); iterPos != target.cend(); iterPos++) std::cout << *iterPos << ' '; std::cout << std::endl; } void main() { std::vector<int> vector = { 1,3,5,7,4,1,2,5,1 }; std::vector<int> result(9); std::cout << "vector--->";Print(vector); std::cout << "result--->";Print(result); std::replace_copy(vector.begin(), vector.end(), result.begin(), 1, 99); std::cout << "vector--->";Print(vector); std::cout << "result--->";Print(result); } | cs |
리턴값)
복사된 값들의 마지막을 가리키는 Iterator를 리턴합니다.
결과)