size_t find (const string& str, size_t pos = 0) const noexcept;(摘自c++官網:std::string::find)
size_t 類型定義在cstddef頭文件中,該文件是C標准庫的頭文件stddef.h的C++版。它是一個與機器相關的unsigned類型,其大小足以保證存儲內存中對象的大小。(摘自百度百科:size_t)
find函數返回所搜索的字符串出現的第一個位置。
第二個pos(position)為可選參數,省略時默認為0。給定pos即從某位置起出現的第一個位置
#include <cstdlib> #include <iostream> #include <windows.h> #include <string> using namespace std; int main() { SetConsoleOutputCP(65001); string s1("hello hello hello"); cout<<s1.find("hello",4)<<endl;//輸出6 system("pause"); }