可以使用字符串的substr函数来提取字符串中的子串。substr函数接受两个参数,第一个参数是子串的起始位置,第二个参数是子串的长度。由于要提取冒号后面的内容,所以起始位置应该是冒号的下一个位置,长度可以设为字符串的长度减去冒号的位置再减去1。

下面是一个示例代码:

#include <iostream>
#include <string>

int main() {
    std::string str = "wdy:32";
    unsigned int temp;

    std::string tempStr = str.substr(str.find(":") + 1);
    temp = std::stoul(tempStr);

    std::cout << "temp: " << temp << std::endl;

    return 0;
}

在上面的代码中,使用find函数找到冒号的位置,然后再用substr函数提取冒号后面的内容。最后使用std::stoul函数将提取到的字符串转换为unsigned int类型的值,并赋给temp变量。

运行上述代码,输出结果为:

temp: 32
``
这里假设字符串格式为 wdy32提取字符串后面的内容赋给unsigned int temp

原文地址: https://www.cveoy.top/t/topic/iBzw 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录