簡體   English   中英

為什么我不能第二次調用std :: mismatch函數?

[英]Why I can't call std::mismatch function for the second time?

我使用STL的不匹配功能來幫助我查找公用目錄路徑。 為此,我使用multimap :: equal_range獲取相等元素的范圍。

對於我的示例程序(請參閱),我得到了一個向量vPathWithCommonDir,其中填充了3個元素,例如“ C:/ MyProg / Raw /”,“ C:/ MyProg / Subset / MTSAT /”和“ C:/ MyProg / Subset / GOESW /”,則在首次迭代多圖mmClassifiedPaths時。 然后,我將此向量傳遞給FindCommonPath函數,並返回所需的公共路徑“ C:/ MyProg”。 第二次循環時,由於只有一個元素,因此不必調用FindCommonPath函數。 當第三次迭代時,我得到一個向量vPathWithCommonDir,其中填充了2個元素,即“ D:/ Dataset / Composite /”和“ D:/ Dataset / Global /”。 當我第二次調用通過vPathWithCommonDir傳遞的FindCommonPath函數時,發生了致命錯誤。 我無法解決這個問題。

你能幫我嗎? 非常感謝你!

// 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 -它不會進行任何檢查。

解決方法是在范圍之間進行距離檢查,並提供較小的范圍作為第一個范圍,較大的范圍作為第二個范圍。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM