c++ - 为什么我不能第二次调用 std::mismatch 函数?

标签 c++ stl

我使用 STL 的不匹配功能来帮助我找到共同的目录路径。为此,我使用 multimap::equal_range 来获取相等元素的范围。

对于我的示例程序(请参阅引用资料),我得到了一个 vector vPathWithCommonDir,其中包含 3 个元素,例如“C:/MyProg/Raw/”、“C:/MyProg/Subset/MTSAT/”和“C :/MyProg/Subset/GOESW/",当第一次迭代 multimap mmClassifiedPaths 时。然后我将这个 vector 传递给 FindCommonPath 函数,并返回我想要的公共(public)路径“C:/MyProg”。第二次循环时,不需要调用FindCommonPath函数,因为只有一个元素。当第三次迭代时,我得到一个 vector vPathWithCommonDir 填充了 2 个元素,即“D:/Dataset/Composite/”和“D:/Dataset/Global/”。当我第二次调用通过 vPathWithCommonDir 传递的 FindCommonPath 函数时发生 fatal error 。我无法解决这个问题。

你能帮帮我吗?非常感谢!

// TestMismatch.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include <algorithm> 
#include <map>
#include <vector>
#include <string>

std::string FindCommonPath(const std::vector<std::string> & vDirList, char cSeparator) ;

int _tmain(int argc, _TCHAR* argv[])
{   
std::vector<std::string> vDirList;

// Populate the vector list
vDirList.push_back("C:/XML/");
vDirList.push_back("C:/MyProg/Raw/");
vDirList.push_back("C:/MyProg/Subset/MTSAT/");
vDirList.push_back("C:/MyProg/Subset/GOESW/");
vDirList.push_back("D:/Dataset/Composite/");
vDirList.push_back("D:/Dataset/Global/");
vDirList.push_back("E:/Dataset/Mosaic/");

std::multimap<std::string, std::string> mmClassifiedPaths;

for (std::vector<std::string>::iterator it = vDirList.begin(); it != vDirList.end(); it++)
{   
    std::string sPath = *it;
    std::string::iterator itPos;

    std::string::iterator itBegin = sPath.begin();
    std::string::iterator itEnd = sPath.end();

    // Find the first occurrence of separator '/'
    itPos = std::find( itBegin, itEnd, '/' );

    // If found '/' for the first time
    if ( itPos != itEnd ) 
    {  
       // Advance the current position iterator by at least 1
       std::advance(itPos, 1);

       // Find the second occurrence of separator '/'
       itPos = std::find( itPos, itEnd, '/' );

       // If found '/' for the second time
       if ( itPos != itEnd ) 
       {  
          std::string sFound = sPath.substr(0, itPos - itBegin);
          mmClassifiedPaths.insert( std::pair<std::string, std::string>(sFound, sPath) );
       }
    }
}

//std::multimap<std::string, std::string>::iterator it;
std::vector<std::string> vPathToWatch;
std::pair<std::multimap<std::string, std::string>::iterator,    std::multimap<std::string, std::string>::iterator> pRet;

for (std::multimap<std::string, std::string>::iterator it = mmClassifiedPaths.begin(); 
     it != mmClassifiedPaths.end(); it++)
{   
    size_t nCounter = (int)mmClassifiedPaths.count(it->first);
    pRet = mmClassifiedPaths.equal_range(it->first);

    if (nCounter <= 1)
    {  
       vPathToWatch.push_back(it->second);
       continue;
    }

    std::vector<std::string> vPathWithCommonDir;

    for (std::multimap<std::string, std::string>::iterator itRange = pRet.first; itRange != pRet.second; ++itRange)
    {   
        vPathWithCommonDir.push_back(itRange->second);
    }

    // Find the most common path among the passed path(s)
    std::string strMostCommonPath = FindCommonPath(vPathWithCommonDir, '/');

    // Add to directory list to be watched
    vPathToWatch.push_back(strMostCommonPath);

    // Advance the current iterator by the amount of elements in the 
    // container with a key value equivalent to it->first
    std::advance(it, nCounter - 1);
}

return 0;
}

std::string FindCommonPath(const std::vector<std::string> & vDirList, char cSeparator) 
{   
std::vector<std::string>::const_iterator vsi = vDirList.begin();   
int nMaxCharsCommon = vsi->length();   
std::string sStringToCompare = *vsi;   

for (vsi = vDirList.begin() + 1; vsi != vDirList.end(); vsi++) 
{      
    std::pair<std::string::const_iterator, std::string::const_iterator> p = std::mismatch(sStringToCompare.begin(), sStringToCompare.end(), vsi->begin()); 

    if ((p.first - sStringToCompare.begin()) < nMaxCharsCommon)      
       nMaxCharsCommon = p.first - sStringToCompare.begin();
}

std::string::size_type found = sStringToCompare.rfind(cSeparator, nMaxCharsCommon);

return sStringToCompare.substr( 0 , found ) ;
}   

最佳答案

您必须确保提供给 mismatch 的两个迭代器范围内至少有同样多的项目 - 它不进行任何检查。

解决方法是在范围之间进行距离检查,并提供较小的范围作为第一个范围,较大的范围作为第二个范围。

关于c++ - 为什么我不能第二次调用 std::mismatch 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7526861/

相关文章:

c++ - 使用 std::future 无效使用不完整类型

c++ - 常量引用 std::vector

关于 count_if : expected primary-expression before 的 C++ 错误

c++ - C++中的抽象类与接口(interface)

使用 Boost Thread 的 C++ 链接器错误

c++ - 如何使 QString 在 QT 中具有特定大小?

c++ - QImage RGB32 到 QImage RGB24,某种图像的结果不好

c++ - 面向WASM的C++代码(具有dlib和opencv)的编译面临的问题

c++ - 如何修改 C++ 中现有的 STL 查找功能?

c++ - CUDA 扩展 std::vector 以管理主机和设备数据