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