###### tags: `leetcode` # Question 28. Implement strStr() ### Description: Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. ### Solution: ``` string::npos - string::npos作為string的成員函式的一個長度引數時,表示“直到字串結束(until the end of the string)” - 它必須定義為 string::size_type ``` ### AC code ```cpp= #include <string.h> class Solution { public: int strStr(string haystack, string needle) { size_t pos = haystack.find(needle); if(pos==string::npos) return -1; else return pos; } }; ```