Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6605907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T19:20:21+00:00 2026-05-25T19:20:21+00:00

I use STL’s mismatch function to help me to find common directory path. In

  • 0

I use STL’s mismatch function to help me to find common directory path. In doing so, I use multimap::equal_range to get range of equal elements.

For my sample program (please see fro you reference), I got a vector vPathWithCommonDir filled with 3 elements such as “C:/MyProg/Raw/”, “C:/MyProg/Subset/MTSAT/” and “C:/MyProg/Subset/GOESW/”, when iterating the multimap mmClassifiedPaths for the first time. I then passed this vector to FindCommonPath function, and returned a common path “C:/MyProg” what I wanted. When looping for the second time, it’s not necessary to call FindCommonPath function because there is only one element. When iterating for the third time, I got a vector vPathWithCommonDir filled with 2 elements, namely “D:/Dataset/Composite/” and “D:/Dataset/Global/”. A fatal error occurred when I called FindCommonPath function passed with vPathWithCommonDir for the second time. I could not solve this problem.

Would you please help me? Thank you very much!

// 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 ) ;
}   
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T19:20:22+00:00Added an answer on May 25, 2026 at 7:20 pm

    You have to ensure that there are at least as many items in both iterator ranges provided to mismatch – it does not do any checking.

    The fix would be to do a distance check between the ranges and provide the smaller one as the first range and the larger range as second.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use partial application of function arguments so I can use STL's
I'm trying to use stl sort() in a class function. I would like to
Is there a way to use stl algorithms like find() and find_if() in a
I'd like to use stl::set to put some directory paths. The set has some
I'm trying to use stl algorithm for_each without proliferating templates throughout my code. std::for_each
If you use stl containers together with reference_wrappers of POD types, code such as
I'd like to use an STL set in my iPhone app (which is written
I was trying to use an STL list in C++ and I arrived into
What allocators are available out there for use with STL when dealing with small
One of the things that seems to be necessary with use of STL is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.