[STL] fill

STL/변경 가능 시퀀스 알고리즘 2010. 5. 17. 15:32 Posted by zetz
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
31
32
33
34
35
36
37
38
39
/*
 
fill 과 fill_n 알고리즘은 구간에 속하는 모든 위치를 주어진 값으로 채워 넣는다.
fill 과 fill_n 알고리즘의 시간 복잡도는 선형적이다.
 
*/
 
 
#include <iostream>
#include <cassert>
#include <algorithm>
#include <vector>
#include <string>
 
using namespace std;
 
int main()
{
    cout<< "Illustrating the generic fill and fill_n algorithms."
        << endl;
 
    string s("Hello there");
    vector<char> vector1(s.begin(), s.end());
 
    // vector1의 맨 앞부분 다섯 자리를 x로 채운다.
    fill(vector1.begin(), vector1.begin() + 5, 'X');
 
    assert (string(vector1.begin(), vector1.end()) ==
            string("XXXXX there"));
 
    // Y로 그 다음 세 자리를 채운다.
    fill_n(vector1.begin() + 5, 3, 'Y');
 
    assert (string(vector1.begin(), vector1.end()) ==
            string("XXXXXYYYere"));
    cout<< " --- Ok."<< endl;
 
    return 0;
}

'STL > 변경 가능 시퀀스 알고리즘' 카테고리의 다른 글

[STL] remove  (0) 2010.05.17
[STL] random_shuffle  (0) 2010.05.17
[STL] partition  (0) 2010.05.17
[STL] generate  (0) 2010.05.17
[STL] copy  (0) 2010.05.17