溫馨提示×

c++中strstr函數的用法是什么

c++
小億
191
2024-02-22 11:24:27
欄目: 編程語言

在C++中,strstr函數用于在一個字符串中搜索指定子字符串的位置。其原型如下:

char* strstr(const char* str1, const char* str2);

其中,str1是要搜索的字符串,str2是要查找的子字符串。strstr函數會返回指向第一次出現str2的位置的指針,如果找不到則返回nullptr。

示例代碼:

#include <iostream>
#include <cstring>

int main() {
    const char* str1 = "Hello, world!";
    const char* str2 = "world";

    char* result = strstr(str1, str2);

    if (result) {
        std::cout << "Found at position: " << result - str1 << std::endl;
    } else {
        std::cout << "Not found" << std::endl;
    }

    return 0;
}

輸出結果為:

Found at position: 7

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女