我在使用find_last_of时遇到问题

I have problems with using find_last_of

本文关键字:of 遇到 问题 last find      更新时间:2023-10-16

当我试图在文件中找到最后一次出现的"EndProject"时,它看起来像这样:(这是原始VS解决方案文件,没有任何更改):

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ASO", "ASOASO.vcxproj", "{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "asdfasdf", "asdfasdfasdfasdf.vcxproj", "{05527F0D-4B98-4A55-B038-3C60005566CB}"
EndProject
Global
    GlobalSection(SolutionConfigurationPlatforms) = preSolution
        Debug|Win32 = Debug|Win32
        Release|Win32 = Release|Win32
    EndGlobalSection
    GlobalSection(ProjectConfigurationPlatforms) = postSolution
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.ActiveCfg = Debug|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Debug|Win32.Build.0 = Debug|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.ActiveCfg = Release|Win32
        {574117CD-377D-4C5A-8B6C-B0EAFF8CE158}.Release|Win32.Build.0 = Release|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.ActiveCfg = Debug|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Debug|Win32.Build.0 = Debug|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.ActiveCfg = Release|Win32
        {05527F0D-4B98-4A55-B038-3C60005566CB}.Release|Win32.Build.0 = Release|Win32
    EndGlobalSection
    GlobalSection(SolutionProperties) = preSolution
        HideSolutionNode = FALSE
    EndGlobalSection
EndGlobal

我使用的是:

auto end_of_project_manifest = project_file_contents.find_last_of("EndProject");//here project_file_contents is a string filed with contents of this file.

我得到的结果是1308,这很奇怪,因为这个文件总共只有1313个字符。肯定有什么不对劲,但怎么了?

std::string::find_last_of()将查找搜索字符串中任何字符的最后一个出现,而不是整个搜索字符串的最后一次出现:在这种情况下,如果在EndGlobal中找到o

改为使用std::string::rfind()

auto end_of_project_manifest = project_file_contents.rfind("EndProject");