관리 메뉴

Kim's Programming

Algorithm - replace() 본문

STL - Algorithm/Algorithm - Modifying

Algorithm - replace()

Programmer. 2017. 7. 12. 10:48

원형)


1
2
3
template <class ForwardIterator, class T>
  void replace (ForwardIterator first, ForwardIterator last,
                const T& old_value, const T& new_value);
cs



의미)


[Iterator first, Iterator last)사이에 있는 데이터 들에 대해서 old_value와 같은 데이터들을 new_value값으로 바꿉니다.



소스)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#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,};
 
    std::cout << "Vector--->"; Print(vector);
    std::replace(vector.begin(), vector.end(), 199);
    std::cout << "Vector--->"; Print(vector);
}
cs



리턴값)


없음



결과)




'STL - Algorithm > Algorithm - Modifying' 카테고리의 다른 글

Algorithm - replace_copy  (0) 2017.07.12
Algorithm - replace_if()  (0) 2017.07.12
Algorithm - transform()  (0) 2017.07.12
Algorithm - iter_swap()  (0) 2017.07.12
Algorithm - swap_ranges()  (0) 2017.07.12