我在使用find_last_of时遇到问题

时间:2012-02-24 13:05:41

标签: c++ algorithm

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

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "ASO", "ASO\ASO.vcxproj", "{574117CD-377D-4C5A-8B6C-B0EAFF8CE158}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "asdfasdf", "asdfasdf\asdfasdf.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个字符。当然有什么不对,但是什么?

1 个答案:

答案 0 :(得分:11)

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

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

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