언어
[C++] find() / npos
송파감자
2023. 9. 7. 15:48
s1 = "Welcome to the world of Potato";
string word{};
cout << "Enter the word to find: ";
cin >> word;
size_t position = s1.find(word);
if (position != string::npos)
cout << "Found! " << word << " at position : " << position << endl;
else
cout << "Sorry, " << word << " not found" << endl;
C++ 스트링을 사용했을 때, 문자열의 위치를 npos를 써서 찾아보았다.
npos가 뭐냐면 no position이라고 생각하면 쉽다.
위의 코드에서 if문으로 들어간 건 position이 있으니까 위치를 찾은 것이다.