[ create a new paste ] login | about

Link: http://codepad.org/gVdA2H9s    [ raw code | output | fork ]

arunksaha - C++, pasted on Oct 22:
// Arun Saha, 2010-10-21
// http://stackoverflow.com/questions/3986056/get-the-path-difference-between-two-directories

#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

string
mismatch_string( string const & a, string const & b ) {

    string::const_iterator longBegin, longEnd, shortBegin;

    if( a.length() >= b.length() ) {
        longBegin = a.begin();
        longEnd = a.end();
        shortBegin = b.begin();
    }
    else {
        longBegin = b.begin();
        longEnd = b.end();
        shortBegin = a.begin();
    }
    
    pair< string::const_iterator, string::const_iterator >  mismatch_pair = mismatch( longBegin, longEnd, shortBegin );
    return string(  mismatch_pair.first, longEnd );
}

int main() {

    string longer( "/home/benjamin/test/a/1" );
    string shorter( "/home/benjamin/test/" );

    cout << mismatch_string( longer, shorter ) << endl;
    cout << mismatch_string( shorter, longer ) << endl;
}


Output:
1
2
a/1
a/1


Create a new paste based on this one


Comments: