c++查詢特定字符串位置


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");

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM