[c++] 문자열 관련 함수 및 라이브러리

C++에서 문자열을 다루는 데 유용한 내장 함수 및 라이브러리에 대해 알아보겠습니다.

기본 문자열 함수

length 함수

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello";
    int len = str.length();
    std::cout << "문자열 길이: " << len << std::endl;
    return 0;
}

substr 함수

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    std::string substr = str.substr(7, 5);
    std::cout << "잘라낸 문자열: " << substr << std::endl;
    return 0;
}

String 헤더 라이브러리

getline 함수

#include <iostream>
#include <string>

int main() {
    std::string str;
    std::cout << "문자열 입력: ";
    std::getline(std::cin, str);
    std::cout << "입력한 문자열: " << str << std::endl;
    return 0;
}

find 함수

#include <iostream>
#include <string>

int main() {
    std::string str = "Hello, World!";
    size_t found = str.find("World");
    if (found != std::string::npos) {
        std::cout << "찾은 위치: " << found << std::endl;
    } else {
        std::cout << "찾지 못함" << std::endl;
    }
    return 0;
}

이러한 C++ 문자열 함수 및 라이브러리를 이용하여 문자열 처리를 보다 효율적이고 편리하게 할 수 있습니다.

참고 자료: cplusplus.com