string::npos는 C++ 표준 라이브러리에서 제공되는 상수로, <string> 헤더에 정의되어 있습니다. string::npos는 static const size_t npos = -1;로 정의됩니다. string::npos는 주로 문자열 멤버 함수들과 함께 사용됩니다.
이러한 함수들은 검색이 실패했을 때 string::npos를 반환합니다.
string::npos를 사용하는 일반적인 멤버 함수는 다음과 같습니다.
다음은 string::npos가 어떻게 사용되는지 보여주는 예제입니다.
#include <iostream>
#include <string>
int main() {
std::string str = "Hello, world!";
std::string toFind = "world";
size_t pos = str.find(toFind);
if (pos != std::string::npos) {
std::cout << "Found '" << toFind << "' at position " << pos << std::endl;
} else {
std::cout << "'" << toFind << "' not found" << std::endl;
}
return 0;
}
std::string::npos를 반환합니다.