jmc
jmc

Reputation: 51

Diff function on two arrays, in c++/mfc/stl?

Diff function on two arrays (or how to turn Old into New) 

Example
One[]={2,3,4,5,6,7}
Two[]={1,2,3,5,5,5,9}

Example Result
Diff: insert 1 into One[0], One[]={1,2,3,4,5,6,7}
Diff: delete 4 from One[3], One[]={1,2,3,5,6,7}
Diff: modify 6 into 5 in One[4], One[]={1,2,3,5,5,7}
Diff: modify 7 into 5 in One[5], One[]={1,2,3,5,5,5}
Diff: append 9 into One[6], One[]={1,2,3,5,5,5,9}

Need code in c++/mfc/stl/c, Thanks.

Upvotes: 1

Views: 344

Answers (3)

cubicdaiya
cubicdaiya

Reputation: 368

I'm diff library developer with C++.

http://code.google.com/p/dtl-cpp/

Using My diff library, it is possible to calculate the difference between two sequences.

Please see examples/intdiff.cpp about how to use.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490108

Though it's normally done with letters instead of integers, the usual algorithm for computing the Levenstein distance should work just as well here as where it's usually applied.

Upvotes: 1

Gintautas Miliauskas
Gintautas Miliauskas

Reputation: 7892

What you need is a string matching algorithm, usually implemented using dynamic programming (see here).

I'd highly suggest using a library that performs the diff instead of implementing it yourself.

Upvotes: 1

Related Questions