기술 블로그

문자열 자체의 find와 C++ STL인 find 본문

C++ STL

문자열 자체의 find와 C++ STL인 find

parkit 2019. 9. 23. 23:45
728x90
반응형

주석 참고



1
2
3
4
5
6
7
8
9
10
11
12
문자열 asdioeabcedsddio안에 d가 2번 째에 존재합니다.
 
문자열 asdioeabcedsddio안에 z가 존재하지 않습니다.
 
2
 
-1
 
13
 
1
2
cs






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
40
41
42
43
44
45
46
#include <bits/stdc++.h>
 
using namespace std;
 
void string_check(string s, char c)
{
    // C++ STL find는 '문자'만 적용된다. '문자열'은 문자열(여기서는 s)의 find를 이용해야한다.
 
    if (find(s.begin(), s.end(), c) != s.end())
    {
        cout << "문자열 " << s << "안에 " << c << "가 " << find(s.begin(), s.end(), c) - s.begin() << "번 째에 존재합니다.\n";
    }
    else
    {
        cout << "문자열 " << s << "안에 " << c << "가 존재하지 않습니다.\n";
    }
 
    printf("\n");
}
 
int main(void)
{
    //cin.tie(0);
 
    string s = "asdioeabcedsddio";
 
    string_check(s, 'd');
    string_check(s, 'z');
 
    int idx = s.find("dio"); // 문자열 s안에서 "dio"를 찾는다. 없으면 -1을 반환함.
    printf("%d\n\n", idx);
 
    idx = s.find("dioz"); // 문자열 s안에서 "dio"를 찾는다. 없으면 -1을 반환함.
    printf("%d\n\n", idx);
        
    // 참고로 문자열은 0부터 시작한다.(인덱스 번호)
    idx = s.find("dio"3); // 문자열 s의 3번째 원소부터 "dio"를 찾는다. 없으면 -1을 반환함.
    printf("%d\n\n", idx);
 
    // 보너스
    vector<int> v = { 2,3,4,4 };
    cout << find(v.begin(), v.end(), 3- v.begin() << '\n';
    cout << count(v.begin(), v.end(), 4<< '\n';
 
    return 0;
}
cs









728x90
반응형

'C++ STL' 카테고리의 다른 글

vector struct 구조체 정렬(3개 이상)  (0) 2019.06.29
search()  (0) 2019.05.20
count(), count_if()  (0) 2019.05.20
equal()  (0) 2019.05.20
is_sorted()  (0) 2019.05.10