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

180it 2020-10-08 PM 1322℃ 0条

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

}
支付宝打赏支付宝打赏 微信打赏微信打赏

如果文章或资源对您有帮助,欢迎打赏作者。一路走来,感谢有您!

标签: none

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