How do i change my merge algorithm to accept different arguments in c++? -
the original code wrote uses these arguments:
int m = size of sorted list 1
int n = size of sorted list 2
int a[] = sorted list 1
int b[] = sorted list 2
int c[] = merged list of 1 , 2
i asked add code existing file uses these different arguments:
intvectorit start1
intvectorit end1
intvectorit start2
intvectorit end2
intvectorit start3
these variables defined here (n size of first sorted array, m size of second sorted array):
typedef vector<int> intvector; typedef intvector::iterator intvectorit; intvector vector1(n); intvectorit start1,end1,it1; start1 = vector1.begin(); end1 = vector1.end(); intvector vector2(m); intvectorit start2,end2,it2; start2 = vector2.begin(); end2 = vector2.end(); intvector vector3(n+m); intvectorit start3,end3,it3; start3 = vector3.begin(); end3 = vector3.end(); //--the variables version of merge intvector myvector(n+m); intvectorit mystart,myend,myit; mystart = myvector.begin(); myend = myvector.end();
my merge code:
void mymerge(int m, int n, int a[], int b[], int c[]) { int i, j, k = 0; while (i < m && j < n) { if (a[i] <= b[j]) { c[k] = a[i]; i++; } else { c[k] = b[j]; j++; } k++; } if (i < m) { (int p = i; p < m; p++) { c[k] = a[p]; k++; } } else { (int p = j; p < n; p++) { c[k] = b[p]; k++; } } }
if me figure out how take iterators arguments, me out ton. thank in advance.
since sounds homework, won't write whole solution. however, here suggestions on migrating mymerge
:
change signature to
void mymerge( intvectorit astart, intvectorit aend, intvectorit bstart, intvectorit bend, intvectorit cstart, intvectorit cend );
change running indices iterators, e.g.
intvectorit = astart;
change loop stopping condition use iterators, e.g.
i != aend
Comments
Post a Comment