C++ algorithm


이번 글에서는 원소를 수정하는 알고리즘에서 몇가지를 알아본다.



1. fill(begin, end, value)

구간 [begin, end)를 value로 채운다.


fill 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>     // std::cout
#include <algorithm>    // std::fill
#include <vector>       // std::vector
 
int main() {
    std::vector<int> myvector(8);                       // myvector: 0 0 0 0 0 0 0 0
 
    std::fill(myvector.begin(), myvector.begin() + 45);   // myvector: 5 5 5 5 0 0 0 0
    std::fill(myvector.begin() + 3, myvector.end() - 28);   // myvector: 5 5 5 8 8 8 0 0
 
    std::cout << "myvector contains:";
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



2. reverse(begin, end)

구간 [begin, end)의 순서를 역순으로 만든다.


reverse 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>     // std::cout
#include <algorithm>    // std::reverse
#include <vector>       // std::vector
 
int main() {
    std::vector<int> myvector;
 
    // set some values:
    for (int i = 1; i<10++i) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9
 
    std::reverse(myvector.begin(), myvector.end());    // 9 8 7 6 5 4 3 2 1
 
                                                       // print out content:
    std::cout << "myvector contains:";
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



3. rotate(begin, mid, end)

구간 [begin, end)를 mid를 기준으로 왼쪽으로 회전시킨다. 

즉, begin에는 mid값이 들어가고, end-1에는 mid-1의 값이 들어간다.


rotate 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>     // std::cout
#include <algorithm>    // std::rotate
#include <vector>       // std::vector
 
int main() {
    std::vector<int> myvector;
 
    // set some values:
    for (int i = 1; i<10++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9
 
    std::rotate(myvector.begin(), myvector.begin() + 3, myvector.end());
    // 4 5 6 7 8 9 1 2 3
    // print out content:
    std::cout << "myvector contains:";
    for (std::vector<int>::iterator it = myvector.begin(); it != myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



4. swap(a, b)

a와 b에 위치한 값을 바꾼다.


swap 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>     // std::cout
#include <algorithm>    // std::swap
#include <vector>       // std::vector
 
int main() {
 
    int x = 10, y = 20;                              // x:10 y:20
    std::swap(x, y);                              // x:20 y:10
 
    std::vector<int> foo(4, x), bar(6, y);       // foo:4x20 bar:6x10
    std::swap(foo, bar);                          // foo:6x10 bar:4x20
 
    std::cout << "foo contains:";
    for (std::vector<int>::iterator it = foo.begin(); it != foo.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



5. unique(begin, end)

구간 [begin, end)에서 연속되는 같은 값 하나를 제외하고 제거

제거는 하지만 컨테이너의 크기를 줄이지는 않는다. (아래 예제에서 15번째 줄을 보면 제거한 뒤 쪽 공간은 남아있다)

제거한 뒤 end 이터레이터를 반환한다. (그래서 18번째 줄을 보면 end까지 벡터의 크기를 변경한다)


unique 사용 예제

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>     // std::cout
#include <algorithm>    // std::unique, std::distance
#include <vector>       // std::vector
 
bool myfunction(int i, int j) {
    return (i == j);
}
 
int main() {
    int myints[] = { 10,20,20,20,30,30,20,20,10 };           // 10 20 20 20 30 30 20 20 10
    std::vector<int> myvector(myints, myints + 9);
 
    // using default comparison:
    std::vector<int>::iterator it;
    it = std::unique(myvector.begin(), myvector.end());   // 10 20 30 20 10 ?  ?  ?  ?
                                                          //                ^
 
    myvector.resize(std::distance(myvector.begin(), it)); // 10 20 30 20 10
 
                                                          // using predicate comparison:
    std::unique(myvector.begin(), myvector.end(), myfunction);   // (no changes)
 
                                                                 // print out content:
    std::cout << "myvector contains:";
    for (it = myvector.begin(); it != myvector.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
 
    return 0;
}
cs



참조: http://www.cplusplus.com/reference/algorithm/

참고문헌: 뇌를 자극하는 C++ STL

'컴퓨터공학 > STL' 카테고리의 다른 글

[STL] C++ STL algorithm3  (0) 2018.07.30
[STL] C++ STL algorithm1  (0) 2018.07.30
[STL] C++ STL multimap  (0) 2018.07.22
[STL] C++ STL map  (0) 2018.07.22
[STL] C++ STL multiset  (0) 2018.07.22

+ Recent posts