본문 바로가기
C++

[C++] std::find() (문자열 컨테이너에서 특정 문자열 찾기)

by foreverever 2019. 4. 21.
반응형

std::find()

  • find 함수는 iterator 순차열 범위에서 원하는 값을 가진 iterator 반복자를 반환한다.

  • 순차열 범위의 값의 타입은 숫자 혹은 문자, 문자열이다.

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>    //std::find()

using namespace std;

vector<string> str = { "I","want","to","be","rich" };

int main() {
    //rich가 위치한 반복자 찾기
    vector<string>::iterator iter = find(str.begin(), str.end(), "rich");

    //반복자를 통한 값 변경 (rich -> happy)
    *iter = "happy";

    //happy 출력
    cout << *iter;    
}

참고
http://www.cplusplus.com/reference/algorithm/find/

반응형